NSGenericException when adding some restrictions programmatically

It's just my code

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)
}

and I get this exception:

Application termination due to an undetected "NSGenericException" exception, reason: "Cannot activate the restriction with elements <UIView: 0x7fe62e00be70; frame = (0 0; 240 128); autoresize = RM + BM; layer = <CALayer: 0x7fe62e06c8f0 → and <UITableView : 0x7fe62c80be00; frame = (0 0; 375 667); clipsToBounds = YES; autoresize = W + H; gestureRecognizers = <NSArray: 0x7fe62b6a54f0>; layer = <CALayer: 0x7fe62b53aba0>; contentOffset: {0, -64}; contentSize: {0, -64}; contentSize:: 0, -64}; {375, 658}> because they don’t have a common ancestor. Are there any constraint reference positions in different view hierarchies? This is illegal.

+4
1

, 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
}
+3

Source: https://habr.com/ru/post/1622559/


All Articles