bringSubviewToFront 不适用于下拉视图 (UIView)
bringSubviewToFront not working with dropdown view (UIView)
我有一个下拉视图(它是 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
}
}
这可能是由于您的图层是如何从情节提要中定义的,看起来您的标签 "current weight" 正在强制您的下拉视图使其变小。也许为下拉部分定义高度限制?
您的单元格的高度大小需要由您的 dropDownView 重新定义。看来你的cell contentView的height跟你的dropDownView的height没有任何关系可以自行改造
我认为您应该在点击按钮后更新高度。
您可以尝试使用此代码来更新单元格的高度。
cell.contentView.size.height = dropDownView.frame.origin.y + dropDownView.frame.height
我想问题出在您将 genderDropdownView 添加到布局的方式上。
尝试通过
将 genderDropdownView 添加到单元格的主视图
cell.view.addSubview(genderDropdownView)
然后要求单元格的视图将下拉视图置于最前面。
cell.view.bringSubview(toFront: genderDropdownButton)
我有一个下拉视图(它是 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
}
}
这可能是由于您的图层是如何从情节提要中定义的,看起来您的标签 "current weight" 正在强制您的下拉视图使其变小。也许为下拉部分定义高度限制?
您的单元格的高度大小需要由您的 dropDownView 重新定义。看来你的cell contentView的height跟你的dropDownView的height没有任何关系可以自行改造
我认为您应该在点击按钮后更新高度。 您可以尝试使用此代码来更新单元格的高度。
cell.contentView.size.height = dropDownView.frame.origin.y + dropDownView.frame.height
我想问题出在您将 genderDropdownView 添加到布局的方式上。
尝试通过
将 genderDropdownView 添加到单元格的主视图cell.view.addSubview(genderDropdownView)
然后要求单元格的视图将下拉视图置于最前面。
cell.view.bringSubview(toFront: genderDropdownButton)