ios - Swift UIButton .contentVerticalAlignment 属性不起作用
问题描述
我需要将 UIButton 的标题附加到右上角,但是当我使用 .contentVerticalAlignment 属性时,顶部和标题之间有一个空格。
class ViewController: UIViewController {
let button: UIButton = {
let button = UIButton()
button.setTitle("1250 km", for: .normal)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = .green
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(button)
NSLayoutConstraint.activate([
button.widthAnchor.constraint(equalToConstant: 150),
button.heightAnchor.constraint(equalToConstant: 100),
button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
button.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
// button.contentEdgeInsets = UIEdgeInsets(top: 0, left: -0.000001, bottom: 0, right: 0)
button.contentVerticalAlignment = .top
button.contentHorizontalAlignment = .right
}
}
但是当我取消注释这一行时,一切正常,顶部和标题之间没有空格。(注意“左”值)
button.contentEdgeInsets = UIEdgeInsets(top: 0, left: -0.000001, bottom: 0, right: 0)
谁能告诉我更好的解决方案?
解决方案
may be try this
button.contentHorizontalAlignment = .center
推荐阅读
- java - 即使找到路径,BFS Maze Solver 也会返回 false
- android - 如何将预定通知添加到闹钟应用程序 - Flutter
- javascript - 如何在 window.onload 事件后使用 reactjs / nextjs 加载脚本
- python - Pandas 将两个 DataFrame 中单元格中的字符串与规则进行比较
- r - assign(x)、eval(parse(text = "x"))、get(x) 和 deparse(substitute(x)) 有什么区别?
- python - 谁能解释为什么这个列表配对代码不起作用
- teradata - TOAD 中的 Teradata 函数 IFACTIVITYCOUNT 和 LABEL
- ruby-on-rails - 使用 Postgres 数据库测试 Rails 突然失败
- stm32 - 如何解决:插入所有硬件断点失败;你可能请求了太多的硬件断点/观察点
- python - 如果转储失败,Python yaml 将写入空文件