【问题标题】:Add constraint to UIView programmatically以编程方式向 UIView 添加约束
【发布时间】:2017-08-22 07:48:01
【问题描述】:

我想在视图中添加一个较小的子视图并向其添加约束。前导和训练约束应为 50.0,高度和底部应为 80.0。

我以这种方式创建我的子视图

let mySubview = UIView(frame: CGRect(x: 50.0, y: 80.0, width: 540.0, height: 220.0))

然后我尝试使用此代码将其添加到视图中

let topConstraint = NSLayoutConstraint(item: mySubview, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: 80.0)
let bottomConstraint = NSLayoutConstraint(item: mySubview, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: 80.0)
let leadingConstraint = NSLayoutConstraint(item: mySubview, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1, constant: 50.0)
let trailingConstraint = NSLayoutConstraint(item: mySubview, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1, constant: 50.0)

mySubview.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(mySubview)
self.view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
self.view.layoutIfNeeded()

但是这些禁忌没有任何效果。该视图在 iPad 上看起来不错,可能是因为第一个 init CGRect(x: 50.0, y: 80.0, width: 540.0, height: 220.0),但在较小的屏幕上它看起来非常大。我应该尝试根据屏幕初始化 UIView 还是可以通过添加约束来解决这个问题?

【问题讨论】:

    标签: ios swift uiview nslayoutconstraint


    【解决方案1】:

    你需要这个:

    mySubview.translatesAutoresizingMaskIntoConstraints = false
    

    translatesAutoresizingMaskIntoConstraints 是一个布尔值, 确定视图的自动调整大小掩码是否转换为 自动布局约束。

    【讨论】:

      【解决方案2】:

      你应该尝试使用http://snapkit.io/docs/

      let box = UIView()
      superview.addSubview(box)
      
      box.snp.makeConstraints { (make) -> Void in
          make.top.equalTo(superview).offset(80)
          make.left.equalTo(superview).offset(50)
          make.bottom.equalTo(superview).offset(-80)
          make.right.equalTo(superview).offset(-50)
      }
      

      【讨论】:

        【解决方案3】:

        由于您想使用自动布局,因此在没有框架的情况下实例化子视图就足够了(因为框架的计算考虑了约束):let mySubview = UIView()

        然后你必须为你的子视图设置translatesAutoresizingMaskIntoConstraintsfalse

        您还必须将底部约束的常量更改为 -80,并将尾随约束的常量更改为 -50。如果你想保持正常数,你也可以切换项目(self.view / mySubview):

        let bottomConstraint = NSLayoutConstraint(item: self.view, attribute: .bottom, relatedBy: .equal, toItem: mySubview, attribute: .bottom, multiplier: 1, constant: 80.0)
        let trailingConstraint = NSLayoutConstraint(item: self.view, attribute: .trailing, relatedBy: .equal, toItem: mySubview, attribute: .trailing, multiplier: 1, constant: 50.0)
        

        最后但同样重要的是,您可以删除self.view.layoutIfNeeded() 行。

        【讨论】:

          【解决方案4】:

          您忘记将translatesAutoresizingMaskIntoConstraints 设置为false

          let mySubview = UIView(frame: CGRect(x: 50.0, y: 80.0, width: 540.0, height: 220.0))
          
          mySubview.translatesAutoresizingMaskIntoConstraints = false
          

          【讨论】:

            【解决方案5】:

            我觉得这样可以解决问题

            mySubview.translatesAutoresizingMaskIntoConstraints = false
            

            像这样

            let mySubview: UIView = {
                let thisView = UIView(frame: CGRect(x: 50.0, y: 80.0, width: 540.0, height: 220.0))
                thisView.translatesAutoresizingMaskIntoConstraints = false
                return thisView
            }()
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多