2

我加入一個子視圖導航欄上UINavigationBar的子視圖的限制,問題是,即時通訊無法這樣如何編程添加

終止應用程序的限制添加到它.IM越來越崩潰因未捕獲的異常「NSGenericException ',原因:'無法激活項目約束;值:0.000000>和>,因爲它們沒有共同的祖先。約束是否引用不同視圖層次中的項目?這是非法的。「

使用的代碼如下

//create a slider and add it to the view 
     let slider = UISlider() 
     self.navigationController?.navigationBar.addSubview(slider) 

     //pin the slider 20 points from the left edge of the the superview 
     //from the left edge of the slider to the left edge of the superview 
     //superview X coord is at 0 therefore 0 + 20 = 20 position 
     let horizonalContraints = NSLayoutConstraint(item: slider, attribute: 
      .LeadingMargin, relatedBy: .Equal, toItem: view, 
      attribute: .LeadingMargin, multiplier: 1.0, 
      constant: 20) 

     //pin the slider 20 points from the right edge of the super view 
     //negative because we want to pin -20 points from the end of the superview. 
     //ex. if with of super view is 300, 300-20 = 280 position 
     let horizonal2Contraints = NSLayoutConstraint(item: slider, attribute: 
      .TrailingMargin, relatedBy: .Equal, toItem: view, 
      attribute: .TrailingMargin, multiplier: 1.0, constant: -20) 

     //pin 100 points from the top of the super 
     let pinTop = NSLayoutConstraint(item: slider, attribute: .Top, relatedBy: .Equal, 
      toItem: view, attribute: .Top, multiplier: 1.0, constant: 100) 

     //when using autolayout we an a view, MUST ALWAYS SET setTranslatesAutoresizingMaskIntoConstraints 
     //to false. 
     slider.translatesAutoresizingMaskIntoConstraints = false 
     slider.backgroundColor = UIColor.redColor() 
     //IOS 8 
     //activate the constrains. 
     //we pass an array of all the contraints 
     NSLayoutConstraint.activateConstraints([horizonalContraints, horizonal2Contraints,pinTop]) 

上面的代碼工作正常,如果我使用的線路view.addSubview(slider) 而不是

self.navigationController?.navigationBar.addSubview(slider) 

但這個想法是在一個子視圖上添加約束導航欄 。 歡迎任何想法

回答

0

由於已經聲明的例外情況,導航欄不是「視圖」的子視圖。它屬於導航控制器。 你可以做的是使用導航欄的上海華:

let slider = UISlider() 
self.navigationController?.navigationBar.addSubview(slider) 
let targetView = self.navigationController?.navigationBar.superview 

let horizonalContraints = NSLayoutConstraint(item: slider, attribute: 
    .LeadingMargin, relatedBy: .Equal, toItem: targetView, 
    attribute: .LeadingMargin, multiplier: 1.0, 
    constant: 20) 

let horizonal2Contraints = NSLayoutConstraint(item: slider, attribute: 
    .TrailingMargin, relatedBy: .Equal, toItem: targetView, 
    attribute: .TrailingMargin, multiplier: 1.0, constant: -20) 


let pinTop = NSLayoutConstraint(item: slider, attribute: .Top, relatedBy: .Equal, 
    toItem: targetView, attribute: .Top, multiplier: 1.0, constant: 10) 


slider.translatesAutoresizingMaskIntoConstraints = false 
slider.backgroundColor = UIColor.redColor() 

NSLayoutConstraint.activateConstraints([horizonalContraints, horizonal2Contraints,pinTop]) 

,消除異常,並可能看起來像它做你想要什麼,但這絕不是一個好的解決方案。如果您需要導航欄內的滑塊,請將其添加到導航項。如果您想要將其添加到導航欄中,請將其添加到您的視圖中,並將約束條件設置爲頂部佈局指南。