我试图在单击 TableView 上的按钮时显示弹出窗口,使用 SCLAlertView-Swift图书馆。
我还尝试定义约束,以便旋转设备时尺寸正确。但是,它给了我以下错误:
'UIViewLayoutMarginsGuide'.leading"> because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
我想我正在尝试从 TableViewController 获取 View ,因为我从该类调用此代码。
let alert = SCLAlertView()
alert.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
let margins = view.layoutMarginsGuide
alert.view.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
alert.view.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
alert.view.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 8).isActive=true
alert.view.topAnchor.constraint(equalTo: view.topAnchor, constant: 8).isActive=true
alert.showInfo("Login", subTitle: "")
为什么它说我引用了不同 View 层次结构中的项目?我该怎么做才能解决这个问题?
提前谢谢您。
最佳答案
查看 SCLAlertView
的实现 here ,它似乎是作为 UIApplication.shared.keyWindow
的 subview 添加的。
我建议将约束挂接到应用程序的 UIWindow
上,而不是 TableViewController
的 View 上。
let rv = UIApplication.shared.keyWindow! as UIWindow
let margins = rv.layoutMarginsGuide
关于ios - 使用弹出库时的不同 View 层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40961010/