Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Certain view configurations can cause center button constraints to disappear #16

Open
dodgecm opened this issue Oct 10, 2017 · 6 comments

Comments

@dodgecm
Copy link
Contributor

dodgecm commented Oct 10, 2017

I ran into this doozy of an issue when setting "Hides Bottom Bar on Push" in one of my view controllers. When you wrap a tab controller around a navigation controller, and then push a VC with that setting onto the nav controller, Apple will helpfully completely unmount the tab bar from the view stack for a moment, and in doing so nuking the layout constraints on the center button.

I modified the storyboard in your example app to reproduce the issue.

I also took a stab at fixing it; this solution works for anything above iOS 9.0. I don't support below that in my own app, so I didn't try to fix the legacy autolayout contraint use case.

if #available(iOS 9.0, *) {
    NSLayoutConstraint.activate([
      button.centerXAnchor.constraint(equalTo: tabBar.centerXAnchor),
      button.bottomAnchor.constraint(equalTo: tabBar.bottomAnchor),
      button.widthAnchor.constraint(equalToConstant: unselectedImage.size.width),
      button.heightAnchor.constraint(equalToConstant: unselectedImage.size.height)
    ])
}
@hsoi
Copy link
Member

hsoi commented Oct 11, 2017

@dodgecm So you're saying this is as opposed to the current button auto layout constraints that bind against the view?

https://github.com/HsoiEnterprises/HELargeCenterTabBarController/blob/master/HELargeCenterTabBarController/HELargeCenterTabBarController.swift#L148-L155

        if #available(iOS 11.0, *) {
            NSLayoutConstraint.activate([
                button.centerXAnchor.constraint(equalTo: view.centerXAnchor),
                button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
                button.widthAnchor.constraint(equalToConstant: unselectedImage.size.width),
                button.heightAnchor.constraint(equalToConstant: unselectedImage.size.height)
            ])
        } else {

To replace the above with your suggestion? Or are you suggesting that this work with the button constraints need to be applied and reapplied, because the tab bar gets blown away?

Also, just to be totally clear: when you say "wrap a tab controller around a navigation controller" are you saying the parent VC is the tab controller and a nav controller is embedded within the tab controller? Or that the parent is a nav controller and you're embedding a tab controller within the nav controller? I'm assuming the former (since I don't think you can even do the latter), but I just want to make 100% sure I'm clearly understanding you.

@dodgecm
Copy link
Contributor Author

dodgecm commented Oct 11, 2017

@hsoi Yes, I'm suggesting that binding the constraints against the tabBar (instead of the TabBarVC) is a better solution; you could also try to detect when the constraints are getting disabled and try to re-enable them, but I tried that first and had a lot of difficulty getting it to work.

And yes, I meant the former. I have a Tab Controller, and then one of the tabs is a Nav controller. Pushing a view that has Hides Bottom Bar on Push set onto that Nav controller will trigger the issue. If my explanation is unclear, I suggest checking out the example I linked in the original comment, since that project reproduces the bug within your demo app.

@hsoi
Copy link
Member

hsoi commented Oct 14, 2017

@dodgecm OK. Hrm.

Yeah, I could see that constraining vs. the tab bar is actually the more semantically correct way to go, since this is about "replacing/overriding" the tab.

If you could make a pull-request, that'd be the fastest way to get this all done and integrated. Else I'll get to it as soon as I can.

@dodgecm
Copy link
Contributor Author

dodgecm commented Oct 15, 2017

@hsoi Do you plan on supporting below iOS 9.0? If so I can just set up a PR for my solution. Otherwise I'll have to figure out how to make it work with the legacy constraint API too.

@hsoi
Copy link
Member

hsoi commented Oct 15, 2017

@dodgecm that's fine. Frankly with iOS 11 out. I'm even OK if iOS 10 becomes the minimum.

@dodgecm
Copy link
Contributor Author

dodgecm commented Oct 15, 2017

@hsoi #17

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants