简体   繁体   English

IOS 15 UIButton右对齐不起作用

[英]IOS 15 UIButton right alignment not working

In IOS 15, Even after setting button text alignment to right, it leaves little space after the button title text.在 IOS 15 中,即使将按钮文本对齐设置为右对齐,按钮标题文本之后也几乎没有空间。 As shown in the following image it leaves the space after the word Test.如下图所示,它在单词 Test 之后留下空格。 How can I remove this space?我怎样才能删除这个空间? I want the letter "t" in the text to touch the trailing of the button.我希望文本中的字母“t”触摸按钮的尾部。

在此处输入图像描述

In ios 14 and below it looks like this在 ios 14 及以下版本中,它看起来像这样

在此处输入图像描述

You can try using this with DispatchQueue您可以尝试将其与DispatchQueue一起使用

button.contentHorizontalAlignment = .left
emailBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)

This is a little trick or you can do it with UIButton.Configuration, set your button like this:这是一个小技巧,或者您可以使用 UIButton.Configuration 来实现,将您的按钮设置为:

let myButton: UIButton = {
    let b = UIButton()
    b.backgroundColor = .white
    b.tintColor = .black
    b.layer.cornerRadius = 8
    b.clipsToBounds = true
    b.setTitle(" My button Test", for: .normal) // space in front of string = space fron text and image
    b.setTitleColor(.black, for: .normal)
    b.titleLabel?.font = .systemFont(ofSize: 17, weight: .regular)
    b.contentHorizontalAlignment = .right
    b.setImage(UIImage(systemName: "bag"), for: .normal)
    b.configuration?.imagePlacement = .leading // use button configuration to add image position
    b.translatesAutoresizingMaskIntoConstraints = false

    return b
}()

This is UIButton.Configuration style:这是 UIButton.Configuration 样式:

let myButton: UIButton = {
    
    var filled = UIButton.Configuration.filled()
    filled.title = "My button Test"
    filled.buttonSize = .large
    filled.baseBackgroundColor = .white
    filled.baseForegroundColor = .black
    filled.cornerStyle = .medium
    filled.image = UIImage(systemName: "bag", withConfiguration: UIImage.SymbolConfiguration(scale: .large))
    filled.imagePlacement = .leading
    filled.imagePadding = 4
    filled.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)

    let b = UIButton(configuration: filled, primaryAction: nil)
    b.contentHorizontalAlignment = .right
    b.translatesAutoresizingMaskIntoConstraints = false

    return b
}()

in viewDidLoad present button and set constraints:在 viewDidLoad 中显示按钮并设置约束:

view.addSubview(myButton)
myButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
myButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
myButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
myButton.widthAnchor.constraint(equalToConstant: 200).isActive = true

This is the result:这是结果:

在此处输入图像描述

try set both:尝试同时设置:

button.titleEdgeInsets = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 00)
button.imageEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 00)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM