4
votes

Simple this is 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'm receiving this exception:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to activate constraint with items <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: {375, 658}> because they have no common ancestor. Does the constraint reference items in different view hierarchies? That's illegal.'

1

1 Answers

3
votes

I solved my problem just by adding the messageView first to the view like this:

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
}