【问题标题】:How to programatically add constraints on uinavigationbar subview如何以编程方式在 uinavigationbar 子视图上添加约束
【发布时间】:2015-10-21 07:26:43
【问题描述】:

我正在向导航栏添加一个子视图,问题是我无法向它添加约束。我正在像这样崩溃

由于未捕获的异常“NSGenericException”而终止应用程序,原因:“无法使用项目激活约束; value: 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)

但想法是在导航栏上的子视图上添加约束。 欢迎任何想法

【问题讨论】:

    标签: ios autolayout uinavigationbar constraints nslayoutconstraint


    【解决方案1】:

    正如已经说明的例外,navigationBar 不是“视图”的子视图。它属于导航控制器。 您可以做的是使用导航栏的超级视图:

    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])
    

    这消除了异常,可能看起来像你想要的那样,但这绝对不是一个好的解决方案。如果您想要导航栏内的滑块,请将其添加到导航项中。如果您希望它位于导航栏下方,请将其添加到您的视图中并为顶部布局指南设置一个约束。

    【讨论】:

      猜你喜欢
      • 2014-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多