如何以编程方式为UINavigationBar子视图添加约束。

3

我正在向导航栏添加子视图,但问题是我无法添加约束。我得到了以下错误:

终止应用程序,因为出现未捕获的异常“NSGenericException”,原因:“无法激活约束项;值:0.000000> and >,因为它们没有共同的祖先。该约束引用了不同视图层次结构中的项目吗?那是违法的。

使用的代码如下:

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

但是想法是在导航栏上的子视图上添加约束条件。 欢迎提出任何想法。
1个回答

2

正如异常已经说明的那样,navigationBar不是'view'的子视图。它属于navigationcontroller。 您可以使用navbar的superview:

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

虽然这样做可以消除异常,看起来似乎达到了您的目的,但这绝不是一个好方法。如果您想要在导航栏中添加滑块,请将其添加到navigationitem中。如果您想要在导航栏下面添加滑块,请将其添加到您的View并设置与顶部布局指南的约束。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接