2014-09-19 116 views
-1

我在XCode6的自动布局中挣扎,所以我试图在代码中设置约束。 我有一个滚动视图,它涵盖了大部分的屏幕。在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)) 
} 

当我运行代码,起初一切正常。但是当我旋转设备时,控制台显示了很多约束错误,图像视图和文本视图的位置和大小完全关闭。

我猜有一个更好的方法来让这个工作正常,但我似乎无法得到它的工作。谁能帮我?

回答

0

试试这个

self.btnLocatie.setTranslatesAutoresizingMaskIntoConstraints(false) 
self.imgAfbeelding.setTranslatesAutoresizingMaskIntoConstraints(false) 
+0

你得到的答案还是不 – Ramesh 2014-09-19 12:49:53

+0

不幸的是,它不是为我做任何事。 我遇到的错误示例: 无法同时满足约束条件。下面列表中的至少一个约束可能是你不想要的。 将尝试通过中断约束来恢复 ... 在UIViewAlertForUnsatisfiableConstraints中创建一个符号断点,以在调试器中捕获这个断点。 – etimm 2014-09-19 13:29:58

+0

您的解决方案不起作用 – etimm 2014-09-19 14:27:28