Alexey_BH
Alexey_BH

Reputation: 143

Unable to simultaneously satisfy constraints when adding UITabBarController into UIView

I programmatically add UITabBarController into UIView. That causes console warnings I can't understand and remove. Despite that, in simulator everything is placed as desired.

    final class MainController: UIViewController {
    
    let searchArea = SearchArea()
    let tabBarContainer = UIView()
    let tabBarControl = TabBarController()
    
    override func viewDidLoad() {

        super.viewDidLoad()

        view.backgroundColor = .systemBackground
        let safeArea = view.safeAreaLayoutGuide
        
        view.addSubview(searchArea)
        searchArea.backgroundColor = .systemBackground
        searchArea.addConstraints(
            .alignTopWithTop(of: safeArea),
            .alignLeadingWithLeading(of: safeArea),
            .alignTrailingWithTrailing(of: safeArea),
            .alignHeight(with: safeArea, mult: 0.05)
        )
        searchArea.setupView()
        
        view.addSubview(tabBarContainer)
        tabBarContainer.backgroundColor = .systemBackground
        tabBarContainer.addConstraints(
            .alignTopWithBottom(of: searchArea),
            .alignLeadingWithLeading(of: safeArea),
            .alignTrailingWithTrailing(of: safeArea),
            .alignBottomWithBottom(of: safeArea)
        )
        
        tabBarControl.view.frame = tabBarContainer.bounds
        tabBarControl.tabBar.backgroundColor = .systemBackground
        tabBarContainer.addSubview(tabBarControl.view)
        addChild(tabBarControl)
        tabBarControl.didMove(toParent: self)
    }
    
}

This causes console output:

2023-06-12 17:55:46.716982+0300 BFB[66775:3057149] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "<NSAutoresizingMaskLayoutConstraint:0x600003fe7340 h=-&- v=--& UINavigationBar:0x12f21ecd0.minY == 0 (active, names: '|':UILayoutContainerView:0x12f21eb30 )>", "<NSAutoresizingMaskLayoutConstraint:0x600003ff1e00 h=-&- v=--& UINavigationBar:0x12f21ecd0.height == 44 (active)>", "<NSLayoutConstraint:0x600003fef110 V:[UINavigationBar:0x12f21ecd0]-(0)-[UIFocusContainerGuide:0x6000023f4870'UINavigationControllerContentFocusContainerGuide'] (active)>", "<NSLayoutConstraint:0x600003feee40 UIFocusContainerGuide:0x6000023f4870'UINavigationControllerContentFocusContainerGuide'.bottom == UILayoutContainerView:0x12f21eb30.bottom (active)>", "<NSLayoutConstraint:0x600003f902d0 'UIView-Encapsulated-Layout-Height' UILayoutContainerView:0x12f21eb30.height == 0 (active)>" )

Will attempt to recover by breaking constraint <NSLayoutConstraint:0x600003fef110 V:[UINavigationBar:0x12f21ecd0]-(0)-[UIFocusContainerGuide:0x6000023f4870'UINavigationControllerContentFocusContainerGuide'] (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

Unfortunately, I do not understand what causes this conflict. Because I do nothing with UINavigationBar - it's created by UITabBarController. This message appears when I add this line of code:

tabBarContainer.addSubview(tabBarControl.view)

Of course, I set translatesAutoresizingMaskIntoConstraints to false (it's not shown in sample code but I did).

Also, I set priority of my constraints to .defaultLow (in order to resolve conflicts with come constraints that are created automatically) but it didn't help.

Upvotes: 0

Views: 91

Answers (0)

Related Questions