UIPickerView放置在视图顶部

时间:2019-01-24 05:02:09

标签: ios swift constraints

单击按钮时,我试图将UIPickerView放在TableView的顶部。我尝试使用情节提要板,但它将表的行向下移动。我想要的是选择器在单击按钮时显示为弹出框,然后在选择选项时消失。

这是我用来使选择器显示的代码:

let picker = UIPickerView()

let container = UILayoutGuide()
view.addLayoutGuide(container)

picker.backgroundColor = UIColor.white
picker.dataSource = self
picker.delegate = self
view.addSubview(picker)
picker.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
picker.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive = true
picker.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
picker.heightAnchor.constraint(equalToConstant: 100).isActive = true
picker.setContentHuggingPriority(UILayoutPriority.required, for: .horizontal)

但是,选择器显示如下:

enter image description here

我不明白为什么选择器视图会一直延伸到最后,因为我将trailingAnchor设置为父视图的trailingAnchor

5 个答案:

答案 0 :(得分:1)

您是否已将选择器视图的translatesAutoresizingMaskIntoConstraints设置为false?除非您这样做,否则它将忽略约束

答案 1 :(得分:0)

如果尾随锚不起作用,请尝试将左,上和右设置为0,并提供一个高度(如果您不想覆盖完整的tableView)。

  

此外,在这些表格的顶部添加pickerView   要施加的约束

Do it like this

如果您希望通过编程方式进行操作,则可以在创建UIPickerView对象时提供约束条件

let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))

答案 2 :(得分:0)

除了布局指南,您还可以直接使用表格视图和相对视图。

编码示例:

    func addPickerView() {
        let picker = UIPickerView()

//        let container = UILayoutGuide()
//        view.addLayoutGuide(container)

        picker.translatesAutoresizingMaskIntoConstraints = false

        picker.backgroundColor = UIColor.red
        picker.dataSource = self
        picker.delegate = self
        view.addSubview(picker)
        picker.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
        picker.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
        picker.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
        picker.heightAnchor.constraint(equalToConstant: 100).isActive = true
        picker.setContentHuggingPriority(UILayoutPriority.required, for: .horizontal)

    }

以上代码将产生以下输出,并将其扩展到其父视图。

enter image description here

答案 3 :(得分:0)

将约束设置为等于父视图,然后显示完美。

let topBarHeight = UIApplication.shared.statusBarFrame.size.height +
        (self.navigationController?.navigationBar.frame.height ?? 0.0)
let picker = UIPickerView()
picker.translatesAutoresizingMaskIntoConstraints = false
let container = UILayoutGuide()
view.addLayoutGuide(container)

picker.backgroundColor = UIColor.lightGray
picker.dataSource = self
picker.delegate = self
view.addSubview(picker)
picker.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
picker.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
picker.topAnchor.constraint(equalTo: view.topAnchor, constant: topBarHeight).isActive = true
picker.heightAnchor.constraint(equalToConstant: 200).isActive = true
picker.setContentHuggingPriority(UILayoutPriority.required, for: .horizontal)

答案 4 :(得分:0)

我在Interface Builder本身中找到了最好的方法。我必须将TableViewController更改为ViewController并将TableView放入其中(将ViewController设置为TableViewDelegate和TableViewDataSource)。这使我可以将PickerView放置在IB上,作为表格单元格顶部的分层视图。然后,可以通过为其创建IBOutlet并适当设置.isHidden属性来隐藏和显示它。

此处有更多信息:Swift: TableView in ViewController