【问题标题】:bringSubviewToFront not working with dropdown view (UIView)bringSubviewToFront 不适用于下拉视图(UIView)
【发布时间】:2018-09-29 12:08:58
【问题描述】:

我有一个下拉视图(它是 UIView,其中完全嵌入了 UITableView)。顶部锚点以编程方式限制为 UIButton 的底部锚点,因此当您触摸按钮时,下拉视图会打开。见下面的代码

但是,我的问题在于打开的下拉视图的高度是150,而视图中唯一显示的部分是UITableViewCell 内的部分(见图),底部隐藏在牢房后面。

func genderDropdownViewConfig() {
    genderDropdownView.backgroundColor = .red
    genderDropdownView.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
    genderDropdownView.translatesAutoresizingMaskIntoConstraints = false
    genderDropdownButton.addSubview(genderDropdownView)
    //genderDropdownButton.bringSubview(toFront: genderDropdownView)
    //tableView.cellForRow(at: IndexPath(row: 2, section: 0))?.superview?.addSubview(genderDropdownView)
    //tableView.cellForRow(at: IndexPath(row: 2, section: 0))?.superview?.bringSubview(toFront: genderDropdownView)

    genderDropdownButton.addSubview(genderDropdownView)
    tableView.bringSubview(toFront: genderDropdownButton)

    genderDropdownView.topAnchor.constraint(equalTo: genderDropdownButton.bottomAnchor).isActive = true
    genderDropdownView.centerXAnchor.constraint(equalTo: genderDropdownButton.centerXAnchor).isActive = true
    genderDropdownView.widthAnchor.constraint(equalTo: genderDropdownButton.widthAnchor).isActive = true
    //genderDropdownHeight = genderDropdownView.heightAnchor.constraint(equalToConstant: 0)
    genderDropdownHeight = genderDropdownView.heightAnchor.constraint(equalToConstant: 0)

    for subview in genderDropdownView.subviews {
        subview.backgroundColor = .clear
    }
}

【问题讨论】:

  • 你是否在tableview的单元格中添加了按钮和下拉视图?
  • @MohamadSheikh 是的

标签: ios swift uiview nslayoutconstraint


【解决方案1】:

我猜问题出在你将genderDropdownView添加到布局的方式上。

尝试将genderDropdownView添加到单元格的主视图中

cell.view.addSubview(genderDropdownView)

然后要求单元格的视图将下拉视图置于前面。

cell.view.bringSubview(toFront: genderDropdownButton)

【讨论】:

    【解决方案2】:

    您的单元格的高度大小需要由您的 dropDownView 重新定义。看来您的单元格内容视图的高度与您的 dropDownView 的高度没有任何关系以进行自我改造。

    我认为您应该在点击按钮后更新高度。 您可以尝试使用此代码来更新单元格的高度。

    cell.contentView.size.height = dropDownView.frame.origin.y + dropDownView.frame.height
    

    【讨论】:

    • 这是要走的路,但我没有这样做,而是重新配置了heightForRowAtIndexPath func,然后调用了tableView.reloadData()
    • 在重新加载整个 tableView 时也是正确的,因为不建议一个单元格的高度。当 dataSource 发生变化时,应该尝试重新加载 tableView。
    • 另外,抱歉,这使得UITableView 无法响应单元格触摸,尽管isUserInteractionEnabled = true - 有什么想法吗?
    • 能否请您将代码放在这里,说明您如何选择?
    【解决方案3】:

    这可能是由于您的图层是如何从情节提要中定义的,看起来您的标签“当前权重”正在迫使您的下拉视图使其自身更小。也许为下拉部分定义一个高度约束?

    【讨论】:

    • 我已经为它定义了一个高度约束。未显示时为停用状态,等于0,显示时为激活状态,等于150
    猜你喜欢
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2019-10-16
    • 2017-03-19
    • 2013-09-21
    • 2012-10-08
    相关资源
    最近更新 更多