当我在UITableViewController内部设置UIActivityIndicatorView的约束时,我的应用程序中出现此错误。当我调用showFooter函数时出现错误:
致命异常:NSGenericException 无法使用锚点激活约束,因为它们没有共同的祖先。约束或其锚点是否引用不同视图层次结构中的项目?这是非法的。
takeDamage :: State PlayerStats Bool
takeDamage = do
ps <- get
let health' = health ps - 10
put $ ps { health = health' } -- update health even if dead!
return (health' > 0)
playRound :: State PlayerStats ()
playRound = do
survived <- takeDamage
if survived
then do
...
else do
...
Backtrace / Debug Log:
select m.*,
(select count(*) from table2 t2 where t2.member_id = m.member_id
) as num_t2,
(select count(*) from table3 t3 where t3.member_id = m.member_id
) as num_t3
from members m;
答案 0 :(得分:0)
您只能在同一视图层次结构中的UIView
之间设置约束。您正在将UIActivityIndicatorView
直接添加到应用程序的关键窗口,然后尝试将约束设置到视图控制器的视图中。这两个UIViews不在同一个视图中,所以你会收到错误。
这是UIActivityIndicatorView
:
这是视图控制器view
的视图层次结构:
要使其有效,您必须在UIActivityIndicatorView
和keyWindow
之间设置约束:
func showFooter(show: Bool) {
guard let keyWindow = UIApplication.shared.keyWindow else { return }
if show {
let loadingFooter = UIActivityIndicatorView(activityIndicatorStyle: .gray)
loadingFooter.backgroundColor = .red
keyWindow.addSubview(loadingFooter)
loadingFooter.startAnimating()
loadingFooter.snp.makeConstraints { make in
make.centerX.equalTo(keyWindow)
make.height.equalTo(20)
make.bottom.equalTo(keyWindow).inset(49)
}
} else {
UIApplication.shared.keyWindow?.subviews.forEach { ($0 as? UIActivityIndicatorView)?.removeFromSuperview() }
}
}
另一件小事。在2个视图的相同锚点之间设置约束而不是时
make.centerX.equalTo(self.view.snp.centerX)
你可以写
make.centerX.equalTo(self.view)
有点短; - )