我試圖構建一個可重用的UIView
,它的寬度應該等於它在Swift中的超級視圖。如何使用自動佈局在Swift中爲可重用的UIView設置約束?
由於其超視圖的大小不同,我認爲我必須爲它設置約束與自動佈局。
但我無法弄清楚如何在Swift中以編程方式執行它。
下面是可重複使用的子視圖代碼:
import UIKit
class bottomMenu: UIView {
@IBOutlet var bottomMenu: UIView!
required init(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
NSBundle.mainBundle().loadNibNamed("bottomMenu", owner: self, options: nil)
bottomMenu.backgroundColor = UIColor.redColor()
//How to make the width of the bottom Menu equal to its superview?
self.addSubview(self.bottomMenu)
}
}
誰能告訴我怎麼做呢?
謝謝
你有自定義用戶界面的xib嗎? – Miknash
是的,我有一個xib。我已經將寬度設置爲320px。因此,當我在iPhone 5上運行它時它可以很好地工作。但是,當我將設備更改爲iPhone 6時,它無法匹配屏幕的寬度。這就是爲什麼我希望它可以與自動佈局一起工作。 –