2014-09-19 213 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