在Swift中的对话框中以编程方式更改UIBUTTON尺寸

发布于 2025-02-07 19:45:11 字数 717 浏览 4 评论 0原文

我在Swift IOS中使用自定义按钮UIBUTTON创建对话框。如何编程更改UIBUTTON尺寸。

class SuccessMessageView: UIView, UITextFieldDelegate {

private var buttonConfirm : UIButton = {
    
    let button = UIButton()
    button.setTitle("Ok", for: .normal)
    button.backgroundColor = UIColor(rgb: PreferenceManager.shared.hasDevice() ? purpleColour : orangeColour)
    button.setTitleColor(.white, for: .normal)
    button.layer.cornerRadius = 10
    button.addTarget(self, action: #selector(confirmFunc), for: .touchDown)
    
    button.translatesAutoresizingMaskIntoConstraints = false
    button.widthAnchor.constraint(equalToConstant: 140.0).isActive = true
    button.frame.size.width = 140
    return button
}
}

I create dialog with custom button UIButton in swift IOS. How can change UIButton size programmatically.

class SuccessMessageView: UIView, UITextFieldDelegate {

private var buttonConfirm : UIButton = {
    
    let button = UIButton()
    button.setTitle("Ok", for: .normal)
    button.backgroundColor = UIColor(rgb: PreferenceManager.shared.hasDevice() ? purpleColour : orangeColour)
    button.setTitleColor(.white, for: .normal)
    button.layer.cornerRadius = 10
    button.addTarget(self, action: #selector(confirmFunc), for: .touchDown)
    
    button.translatesAutoresizingMaskIntoConstraints = false
    button.widthAnchor.constraint(equalToConstant: 140.0).isActive = true
    button.frame.size.width = 140
    return button
}
}

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

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

发布评论

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

评论(3

不必在意 2025-02-14 19:45:11

你能尝试一下吗?只需删除与框架相关的线。

private lazy var buttonConfirm : UIButton = {
    let button = UIButton()
    button.setTitle("Ok", for: .normal)
    button.backgroundColor = .red
    button.setTitleColor(.white, for: .normal)
    button.layer.cornerRadius = 10
    button.addTarget(self, action: #selector(confirmFunc), for: .touchUpInside)

    button.translatesAutoresizingMaskIntoConstraints = false
    button.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
    button.heightAnchor.constraint(equalToConstant: 48).isActive = true
    return button
}()

Could you try this? Just remove the frame-related lines.

private lazy var buttonConfirm : UIButton = {
    let button = UIButton()
    button.setTitle("Ok", for: .normal)
    button.backgroundColor = .red
    button.setTitleColor(.white, for: .normal)
    button.layer.cornerRadius = 10
    button.addTarget(self, action: #selector(confirmFunc), for: .touchUpInside)

    button.translatesAutoresizingMaskIntoConstraints = false
    button.widthAnchor.constraint(equalToConstant: 200.0).isActive = true
    button.heightAnchor.constraint(equalToConstant: 48).isActive = true
    return button
}()
得不到的就毁灭 2025-02-14 19:45:11
 fileprivate func Frame() -> CGRect {
        var circleFrame = CGRect(x: 0, y: 0, width: 2, height: 2)
        return circleFrame
    }
 fileprivate func Frame() -> CGRect {
        var circleFrame = CGRect(x: 0, y: 0, width: 2, height: 2)
        return circleFrame
    }
奈何桥上唱咆哮 2025-02-14 19:45:11

我找到了设置UIBUTTON宽度和高度的解决方案,就像恒定设置一样。

button.anchor(nil, left: nil, bottom: nil, right: nil, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 20, heightConstant: 30)
    button.anchorCenterXToSuperview()

I found the solution for set UIButton width and height programatically like that set with constant.

button.anchor(nil, left: nil, bottom: nil, right: nil, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 20, heightConstant: 30)
    button.anchorCenterXToSuperview()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文