简单这是我的代码
func showMessageView(){
let leftConstraint = messageView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor)
let bottomConstraint = messageView.bottomAnchor.constraintGreaterThanOrEqualToAnchor(self.view.bottomAnchor)
let highConstratin = messageView.heightAnchor.constraintEqualToConstant(44.0)
let widthConstraint = messageView.widthAnchor.constraintGreaterThanOrEqualToAnchor(self.view.widthAnchor)
NSLayoutConstraint.activateConstraints([leftConstraint, bottomConstraint, highConstratin, widthConstraint])
self.view.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(messageView)
}
我收到了这个例外:
由于未捕获的异常'NSGenericException'而终止应用程序,原因:'无法激活带有项目的约束< UIView:0x7fe62e00be70; frame =(0 0; 240 128); autoresize = RM + BM; layer =< CALayer:0x7fe62e06c8f0>>和< UITableView:0x7fe62c80be00; frame =(0 0; 375 667); clipsToBounds = YES; autoresize = W + H; gestureRecognizers =< NSArray:0x7fe62b6a54f0&gt ;; layer =< CALayer:0x7fe62b53aba0&gt ;; contentOffset:{0,-64}; contentSize:{375,658}>因为他们没有共同的祖先。约束是否引用不同视图层次结构中的项目?这是非法的。'
答案 0 :(得分:3)
我通过首先将messageView添加到视图中解决了我的问题:
func showMessageView(){
self.view.addSubview(messageView)
let leftConstraint = messageView.leftAnchor.constraintEqualToAnchor(self.view.leftAnchor)
let bottomConstraint = messageView.bottomAnchor.constraintGreaterThanOrEqualToAnchor(self.view.bottomAnchor)
let highConstratin = messageView.heightAnchor.constraintEqualToConstant(44.0)
let widthConstraint = messageView.widthAnchor.constraintGreaterThanOrEqualToAnchor(self.view.widthAnchor)
NSLayoutConstraint.activateConstraints([leftConstraint, bottomConstraint, highConstratin, widthConstraint])
self.view.translatesAutoresizingMaskIntoConstraints = false
}