中心对齐UIBUTTON XCODE以编程方式

发布于 2025-01-27 10:26:43 字数 670 浏览 4 评论 0原文

我正在尝试以编程方式将Uibutton的文本集中。

就是我希望uibutton的外观(我使用了接口构建器):

uibutton> uibutton

这 是uibutton在编程之后的样子:

uibutton> uibutton

这就是Uibutton的外观就像我以编程方式设置并试图像第一张图片一样将文本设置为中心:

uibutton> uibutton

是我试图将其居中的代码:

previousPageButton.titleLabel?.numberOfLines = 0

previousPageButton.titleLabel?.textAlignment = .center

I’m trying to center the text of UIButton programmatically.

This is what I want the UIButton to look like (I used the interface builder):

UIbutton

This is what the UIButton looks like after I set it programmatically:

UIbutton

And this is what the UIButton looks like after I set it programmatically and tried to center the text like in the first picture:

UIButton

This is the code that I used to try to center it:

previousPageButton.titleLabel?.numberOfLines = 0

previousPageButton.titleLabel?.textAlignment = .center

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2025-02-03 10:26:43

问题是我已将“归因于”接口构建器中的标题设置为。我只需要将其设置为“普通”,现在正在工作。

The problem was I had set up "attributed" to the title in interface builder. I just had to set it to "plain" and it's working now.

伪装你 2025-02-03 10:26:43

使用新按钮配置进行操作,设置您的按钮:

let myButton: UIButton = {
    let b = UIButton()
    b.configuration = UIButton.Configuration.filled()
    b.configuration?.title = "CLICK TO GO TO\n PREVIOUS PAGE"
    b.titleLabel?.textAlignment = .center
    b.configuration?.baseForegroundColor = .white // text color
    b.configuration?.baseBackgroundColor = .black // background color
    b.configuration?.cornerStyle = .small // corner radius
    b.translatesAutoresizingMaskIntoConstraints = false
    
    return b
}()

在ViewDidload集合设置约束:

view.addSubview(myButton)
    myButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    myButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    myButton.heightAnchor.constraint(equalToConstant: 80).isActive = true // set eight of button
    myButton.widthAnchor.constraint(equalToConstant: 200).isActive = true // set width of button

这是结果:

“在此处输入图像说明”

Do it with new button configuration, set your button:

let myButton: UIButton = {
    let b = UIButton()
    b.configuration = UIButton.Configuration.filled()
    b.configuration?.title = "CLICK TO GO TO\n PREVIOUS PAGE"
    b.titleLabel?.textAlignment = .center
    b.configuration?.baseForegroundColor = .white // text color
    b.configuration?.baseBackgroundColor = .black // background color
    b.configuration?.cornerStyle = .small // corner radius
    b.translatesAutoresizingMaskIntoConstraints = false
    
    return b
}()

in viewDidLoad set constraints:

view.addSubview(myButton)
    myButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    myButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    myButton.heightAnchor.constraint(equalToConstant: 80).isActive = true // set eight of button
    myButton.widthAnchor.constraint(equalToConstant: 200).isActive = true // set width of button

This is the result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文