【问题标题】:UIButtons not sticking to UIView edgesUIButtons 不粘在 UIView 边缘
【发布时间】:2016-12-15 22:54:18
【问题描述】:

您好,提前致谢。我正在尝试将两个 UIButtons 绑定到 UIViewControllerview,如下所示:

首先声明它们:

fileprivate var deleteButton: UIButton = UIButton(type: .system)
fileprivate var addButton: UIButton = UIButton(type: .system)

接下来设置它们:

private func setupButtons() {

    deleteButton.setTitle("Delete", for: .normal)
    addButton.setTitle("Add", for: .normal)

    deleteButton.sizeToFit()
    addButton.sizeToFit()

    deleteButton.alpha = 1
    addButton.alpha = 1

    view.addSubview(deleteButton)
    view.addSubview(addButton)

    view.addConstraint(NSLayoutConstraint(item: deleteButton,
                                          attribute: .leading,
                                          relatedBy: .equal,
                                          toItem: view,
                                          attribute: .leading,
                                          multiplier: 1.0,
                                          constant: 0))

    view.addConstraint(NSLayoutConstraint(item: deleteButton,
                                          attribute: .bottom,
                                          relatedBy: .equal,
                                          toItem: view,
                                          attribute: .bottom,
                                          multiplier: 1.0,
                                          constant: 0))

    view.addConstraint(NSLayoutConstraint(item: addButton,
                                          attribute: .trailing,
                                          relatedBy: .equal,
                                          toItem: view,
                                          attribute: .trailing,
                                          multiplier: 1.0,
                                          constant: 0))

    view.addConstraint(NSLayoutConstraint(item: addButton,
                                          attribute: .bottom,
                                          relatedBy: .equal,
                                          toItem: view,
                                          attribute: .bottom,
                                          multiplier: 1.0,
                                          constant: 0))

}

但是运行模拟器会将两个 UIButtons 粘在左上角,默认的 CGRect 帧都分配给它们。

你可能知道我做错了什么吗?我觉得我很接近,但也许它与重新绘制视图有关?

【问题讨论】:

    标签: ios uibutton swift3 nslayoutconstraint


    【解决方案1】:

    您应该将translatesAutoresizingMaskIntoConstraints 设置为false

    // before activate constraint
    
    deleteButton.translatesAutoresizingMaskIntoConstraints = false
    addButton.translatesAutoresizingMaskIntoConstraints = false
    

    另外,您必须设置isActive = true 而不是使用addConstraint 方法

    【讨论】:

    • 谢谢,这正是缺少的。你能讨论一下这里发生了什么吗?
    • NP。自动调整掩码约束完全指定视图的大小和位置。如果 translatesAutoresizingMaskIntoConstraints 等于 true(默认情况下),则意味着您无法添加更多约束而不会发生冲突。这意味着您的约束将不起作用甚至崩溃应用程序。你可以阅读更多here
    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2021-09-29
    • 2014-01-20
    相关资源
    最近更新 更多