我在XCode6中遇到自动布局,所以我试图在代码中设置约束。 我有一个覆盖大部分屏幕的scrollview。在scrollview中,我放置了一个按钮,一个imgview和一个textview。该按钮是一个固定的44px,现在我想在imgview和textview之间拆分滚动视图的左边(导致它们的大小相同)。
这是我的SubViewsDidLoad函数:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width;
let screenHeight = screenSize.height;
let scrollViewTop = self.navigationBar.frame.height + self.navigationBar.frame.minY
let scrollViewBottom = self.toolBar.frame.height
// set scrollView frame
println ("navigationbarTop: ", scrollViewTop)
println ("toolbarHeight: ", scrollViewBottom)
self.scrollView.frame = CGRect(x: 0, y: scrollViewTop, width: screenWidth, height: screenHeight-(scrollViewTop+scrollViewBottom))
// set btnLocation size & constraints
self.view.addConstraint(NSLayoutConstraint(item:self.btnLocatie, attribute:.CenterX, relatedBy:.Equal, toItem:self.view,attribute:.CenterX, multiplier:1, constant:0))
self.view.addConstraint(NSLayoutConstraint(item:self.btnLocatie, attribute:.Top, relatedBy:.Equal, toItem:self.scrollView,attribute:.Top, multiplier:1, constant:8))
self.view.addConstraint(NSLayoutConstraint(item:self.btnLocatie, attribute:.Left, relatedBy:.Equal, toItem:self.scrollView,attribute:.Left, multiplier:1, constant:8))
// set imageview size & constraints
//X - align to center
self.view.addConstraint(NSLayoutConstraint(item: self.imgAfbeelding, attribute:.CenterX, relatedBy:.Equal, toItem:self.view, attribute:.CenterX, multiplier:1, constant:0))
//Top - Scrollview+8
self.view.addConstraint(NSLayoutConstraint(item:self.imgAfbeelding, attribute:.Top, relatedBy:.Equal, toItem:self.btnLocatie, attribute:.Bottom, multiplier:1, constant:+8))
//Left - scrollview leftside+8
self.view.addConstraint(NSLayoutConstraint( item:self.imgAfbeelding, attribute:.Left, relatedBy:.Equal, toItem:self.scrollView, attribute:.Left, multiplier:1, constant:8))
//Size - scrollView size
self.view.addConstraint(NSLayoutConstraint(item:self.imgAfbeelding, attribute:.Bottom, relatedBy:.Equal, toItem:self.scrollView, attribute:.Bottom, multiplier:1, constant:(scrollView.frame.height-btnLocatie.frame.height+16)/2))
// set textview size & constraints
self.view.addConstraint(NSLayoutConstraint( item:self.txtTekst, attribute:.Left, relatedBy:.Equal, toItem:self.scrollView, attribute:.Left, multiplier:1, constant:0))
self.view.addConstraint(NSLayoutConstraint(item: self.txtTekst, attribute:.Top, relatedBy:.Equal, toItem:self.imgAfbeelding, attribute:.Bottom, multiplier:1, constant:+8))
self.view.addConstraint(NSLayoutConstraint(item:self.txtTekst, attribute:.Bottom, relatedBy:.Equal, toItem:self.scrollView, attribute:.Bottom, multiplier:1, constant:scrollView.frame.height-8))
}
当我运行代码时,一开始看起来一切都很好。但是当我旋转设备时,控制台会显示很多约束错误,并且图像视图和textview的位置和大小完全关闭。
我猜测有更好的方法让它正常工作,但我似乎无法让它工作。任何人都可以帮助我吗?
答案 0 :(得分:0)
试试这个
self.btnLocatie.setTranslatesAutoresizingMaskIntoConstraints(false)
self.imgAfbeelding.setTranslatesAutoresizingMaskIntoConstraints(false)