簡體   English   中英

以編程方式向UIView添加約束

[英]Add constraint to UIView programmatically

我想在視圖中添加一個較小的子視圖並為其添加約束。 引導約束和訓練約束應為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還是可以通過添加約束來解決此問題?

你需要這個:

mySubview.translatesAutoresizingMaskIntoConstraints = false

translationsAutoresizingMaskIntoConstraints是一個布爾值,它確定視圖的自動調整大小蒙版是否轉換為自動布局約束。

由於要使用自動布局,因此無需框架即可實例let mySubview = UIView()視圖就足夠了(因為考慮到約束let mySubview = UIView()計算框架): let mySubview = UIView()

然后,您必須將子視圖的translatesAutoresizingMaskIntoConstraints設置為false

您還必須將底部約束的常量更改為-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()行。

您應該嘗試使用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)
}

您忘記將translatesAutoresizingMaskIntoConstraints設置為false

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

mySubview.translatesAutoresizingMaskIntoConstraints = false

我認為這可以解決問題

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
}()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM