代码之家  ›  专栏  ›  技术社区  ›  rhlsthrm

在视图顶部放置uipickerview

  •  0
  • rhlsthrm  · 技术社区  · 6 年前

    单击按钮时,我正试图将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 作为父视图的 拖曳锚 .

    5 回复  |  直到 6 年前
        1
  •  0
  •   Shubham Bakshi    6 年前

    如果 尾锚 不工作,请尝试设置 左、上和右 到0和 同时提供高度 (如果您不想覆盖整个表格视图)。

    另外,将pickerview添加到TableView的顶部 要应用的约束

    Do it like this

    如果您希望通过编程实现,则可以在创建 UIPickerView 对象

    let pickerView = UIPickerView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50))
    
        2
  •  0
  •   f3dm76    6 年前

    你准备好了吗? translatesAutoresizingMaskIntoConstraints 您的选取器视图到 false ?它忽略约束,除非您这样做

        3
  •  0
  •   Jarvis The Avenger adri    6 年前

    与布局指南不同,您可以直接使用表视图作为相对视图。

    编码示例:

        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

        4
  •  0
  •   sachin datarkar    6 年前

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

    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)
    
        5
  •  0
  •   rhlsthrm    6 年前

    我在接口生成器本身中找到了实现这一点的最佳方法。我必须将TableViewController更改为ViewController,并将TableView放入其中(将ViewController设置为TableViewDelegate和TableViewDataSource)。这允许我将PickerView放在ib上,作为表单元格顶部的分层视图。然后我可以通过为它创建一个iboutlet并设置 .isHidden 财产适当。

    更多信息在这里: Swift: TableView in ViewController