我试图将一种观点带到另一种观点面前。该 View 是隐藏的,点击按钮后将显示。这是我的层次结构:
我需要放在前面的 View 是信息 View
我尝试过这样做:
infoView.superview?.bringSubview(toFront: infoView)
但是没用 我尝试过使用 z 索引
infoView.layer.zPosition = 100
但也没有用...
有什么想法吗?
更新: 这是正在运行的按钮的代码:
@IBOutlet var infoButtons: [UIButton]!
@IBAction func infoButtonPressed(_ sender: UIButton) {if sender.tag == 8 {
if constraint.count > 0 {
NSLayoutConstraint.deactivate(constraint)
constraint.removeAll()
}
let alignYConstraint = NSLayoutConstraint(item: infoView, attribute: .centerY, relatedBy: .equal, toItem: infoButtons[7], attribute: .centerY, multiplier: 1.0, constant: 0)
NSLayoutConstraint.activate([alignYConstraint])
constraint.append(alignYConstraint)
self.infoView.textView.scrollRangeToVisible(NSMakeRange(0, 0))
infoView.isHidden = false
infoView.superview?.bringSubview(toFront: infoView)
}
更新:
我有一个 nib 文件,已添加到 infiView 中 这是 Xib 的代码:
import UIKit
class InfoView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet weak var textView: UITextView!
var onClickCallback: (() -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed("InfoView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
textView.backgroundColor = UIColor.clear
}
@IBAction func closeInfoViewPressed(_ sender: Any) {
onClickCallback?()
}
}
在另一个 VC 中,我添加一个 View 并将其分配给 InfoView 类。 infoView 被隐藏,点击后变为可见。 我试图在显示时将其放在标题前面(这不是导航 Controller ,而是自定义 View )。看图:
最佳答案
右键单击 Storyboard层次结构中的 UIButton,以查明您的按钮是否具有牢固的连接。很多时候,人们陷入困境时就是这种情况。您可能必须将按钮从 Storyboard重新连接到代码。您不必为此更改 z-index。第一行代码应该可以工作,具体取决于您将代码放置在文件中的位置。
关于ios - Swift - 查看层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45257725/