在包含导航栏

时间:2016-03-06 13:23:38

标签: ios swift uiviewcontroller uipickerview nslayoutconstraint

Stack Overflow上有各种各样的答案,但我似乎无法让它发挥作用。

我希望UIPickerView的宽度等于superview框架宽度的90%,高度为superview的35% 框架高度,水平居中 并且让它的顶部等于超级视图的顶部但在导航栏下面(最后一个约束是我遇到的困难)

目前,选择器视图的顶部似乎位于导航栏的后面。我尝试添加edgesForExtendedLayout = .TopextendedLayoutIncludesOpaqueBars = false,以便选择器视图位于导航栏下方。我也尝试在约束中添加一个常量 NSLayoutConstraint(item: somePickerView, attribute: .Top, relatedBy: .Equal, toItem: superview, attribute: .TopMargin, multiplier: 1.0, constant: navigationController?.navigationBar.frame.height)但这并没有足够推动选择者的观点。

class ViewController: UIViewController {

    let superview = self.view

    override func viewDidLoad() {

        super.viewDidLoad()

        edgesForExtendedLayout = .Top
        extendedLayoutIncludesOpaqueBars = false

        somePickerView = UIPickerView()
        somePickerView.translatesAutoresizingMaskIntoConstraints = false

        superview.addSubview(somePickerView)

        superview.addConstraint(NSLayoutConstraint(item: somePickerView, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .Width, multiplier: 1, constant: superview.frame.width * 0.90))
        superview.addConstraint(NSLayoutConstraint(item: somePickerView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1, constant: superview.frame.height * 0.35))
        superview.addConstraint(NSLayoutConstraint(item: somePickerView, attribute: .CenterX, relatedBy: .Equal, toItem: superview, attribute: .CenterX, multiplier: 1, constant: 0))

        // the following constraint's not working as expected:
        superview.addConstraint(NSLayoutConstraint(item: somePickerView, attribute: .Top, relatedBy: .Equal, toItem: superview, attribute: .TopMargin, multiplier: 1.0, constant: 0))

    }
}

关于将选择器视图放在导航栏顶部但在导航栏下方的约束的任何建议?

0 个答案:

没有答案