badoo / Chatto

A lightweight framework to build chat applications, made in Swift
MIT License
4.48k stars 592 forks source link

Constraints problem when adding DemoChatViewController as a child view controller #494

Open smiles-p opened 6 years ago

smiles-p commented 6 years ago

I am trying to add DemoChatViewController(subclass of BaseChatViewController) into a view as a child view controller:

addChildViewController(child) child.view.translatesAutoresizingMaskIntoConstraints = false view.addSubview(child.view) child.didMove(toParentViewController: self)

view.addConstraints([ NSLayoutConstraint(item: child.view, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0), NSLayoutConstraint(item: child.view, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0), NSLayoutConstraint(item: child.view, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0), NSLayoutConstraint(item: child.view, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0) ])

But I got runtime error: Unable to simultaneously satisfy constraints. "<_UILayoutSupportConstraint:0x600000285960 _UILayoutSpacer:0x6000005cce40'UIVC-topLayoutGuide'.height == 0 (active)>", "<_UILayoutSupportConstraint:0x600000284f60 V:|-(0)-[_UILayoutSpacer:0x6000005cce40'UIVC-topLayoutGuide'] (active, names: '|':Chatto.BaseChatViewControllerView:0x7fa67342a940 )>", "<NSAutoresizingMaskLayoutConstraint:0x604000093ce0 h=-&- v=-&- UIView:0x7fa67342a560.height == UIView:0x7fa67342a750.height (active)>", "<NSAutoresizingMaskLayoutConstraint:0x604000288e80 h=--& v=--& UIView:0x7fa673373520.height == 0 (active)>", "<NSLayoutConstraint:0x60000009ea50 V:[_UILayoutSpacer:0x6000005cce40'UIVC-topLayoutGuide']-(>=0)-[UIView:0x7fa66df6f110] (active)>", "<NSLayoutConstraint:0x600000284d30 V:[UIView:0x7fa66df6f110]-(0)-| (active, names: '|':Chatto.BaseChatViewControllerView:0x7fa67342a940 )>", "<NSLayoutConstraint:0x6040002972a0 UIView:0x7fa66b613d30.height == 44 (active)>", "<NSLayoutConstraint:0x604000483cf0 V:|-(0)-[ChattoAdditions.ExpandableTextView:0x7fa66c167000] (active, names: '|':ChattoAdditions.ChatInputBar:0x7fa67323d040 )>", "<NSLayoutConstraint:0x604000480000 V:[ChattoAdditions.ExpandableTextView:0x7fa66c167000]-(0)-[UIView:0x7fa66b613d30] (active)>", "<NSLayoutConstraint:0x604000486810 V:[UIView:0x7fa66b613d30]-(0)-| (active, names: '|':ChattoAdditions.ChatInputBar:0x7fa67323d040 )>", "<NSLayoutConstraint:0x604000093ec0 UIView:0x7fa66df6f110.top == ChattoAdditions.ChatInputBar:0x7fa67323d040.top (active)>", "<NSLayoutConstraint:0x604000287530 V:[ChattoAdditions.ChatInputBar:0x7fa67323d040]-(0)-| (active, names: '|':UIView:0x7fa66df6f110 )>", "<NSLayoutConstraint:0x604001286bd0 V:|-(0)-[Chatto.BaseChatViewControllerView:0x7fa67342a940] (active, names: '|':UIView:0x7fa67342a560 )>", "<NSLayoutConstraint:0x604001286b30 Chatto.BaseChatViewControllerView:0x7fa67342a940.bottom == UIView:0x7fa67342a560.bottom (active)>", "<NSLayoutConstraint:0x60000029c660 V:|-(0)-[UIView:0x7fa67342a750] (active, names: '|':UIView:0x7fa673373520 )>", "<NSLayoutConstraint:0x60000009f6d0 UIView:0x7fa67342a750.bottom == UIView:0x7fa673373520.bottom (active)>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x6040002972a0 UIView:0x7fa66b613d30.height == 44 (active)>

Any suggestion? Thanks in advance. Cheers

rinat-enikeev commented 6 years ago

@smiles-p same issue for me. Have you managed to fix it?

psi-gh commented 3 years ago

I got constrains error too when adding it (properly) as child VC. Tested only programmatically. No Interface builder.

wiruzx commented 3 years ago

Thanks for submitting the issue! Would anyone like to try to fix this issue and submit a pull request?

psi-gh commented 3 years ago

I got this working by calling this method in container VC (created from the code btw) self.display(contentController: chatViewController, in: self.view) where display method is

extension UIViewController {
    func display(contentController content : UIViewController, in view: UIView) {
        let containerView = view
        let contentView = content.view!

        self.addChild(content)
        content.view.translatesAutoresizingMaskIntoConstraints = false
        containerView.addSubview(content.view)
        content.didMove(toParent: self)

        self.view.addConstraints([
            NSLayoutConstraint(item: contentView, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: contentView, attribute: .bottom, relatedBy: .equal, toItem: containerView, attribute: .bottom, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: contentView, attribute: .leading, relatedBy: .equal, toItem: containerView, attribute: .leading, multiplier: 1, constant: 0),
            NSLayoutConstraint(item: contentView, attribute: .trailing, relatedBy: .equal, toItem: containerView, attribute: .trailing, multiplier: 1, constant: 0)
        ])
    }
}

I don't know why it didn't work earlier. I have a feeling like I didn't change anything and it just started working 😂 So maybe there is no issue with Chatto and just I messed up with the container logic...