HsoiEnterprises / HELargeCenterTabBarController

A Swift UITabBarController with a larger center tab.
Other
45 stars 7 forks source link

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

Open dodgecm opened 6 years ago

dodgecm commented 6 years ago

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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

@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 commented 6 years ago

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

dodgecm commented 6 years ago

@hsoi https://github.com/HsoiEnterprises/HELargeCenterTabBarController/pull/17