1
votes

I'm having a frustrating time getting an auto layout issue to work. Hopefully some fresh eyes on the problem will be better at finding a solution...

I have a UITabBarController in a storyboard file, and in the first view controller there is a MKMapView and a button: This is how the view looks initially in portrait mode, as it should.

The button is constrained to the bottom of the View Controller's view's safe area. The MKMapView is constrained to its superview. And this works fine so far.

The problem comes when I rotate the phone. In the UITabBarController's viewWillTransitionToSize:withTransitionCoordinator: method, I call [self.tabBar setHidden:(size.height < kMinViewSize)] in order to hide the tab bar when the view's height is too small.

After hiding the tab bar, this is what I get: Tab bar is hidden, but neither the map view nor button shift to fill the void

And then when I rotate back to portrait mode again, the tab bar unhides as it should, but the layout gets screwed up once again: Tab bar unhidden, now it covers the map view and button

And, finally, if I select another tab, then come back to the map tab, the button moves back to the correct location.

Am I missing something obvious? Thanks in advance for your help!

2
Are you sure that your constraints are set up as you said? In a quick demo project with the same setup everything works as expected.André Slotta
Thanks André. Weird. I must have screwed something else up in the storyboard, because the constraints are definitely right. I'll try making a demo project and comparing the two.RL2000
try to call setNeedsLayout() and layoutIfNeeded() in viewDidLayoutSubviews()Vadim Kozak
Tried all of those before. And just now I found the problem! I feel like a fool...RL2000

2 Answers

0
votes

Well, I figured it out and it wasn't anything too obvious. The self.tabBar setHidden: call was contained in the animation block of [coordinator animateAlongsideTransition:]. I took the animation block out, and everything worked fine.

Thank you, Andre Slotta for your comment-- that at least helped me realize something was wrong in the code if not in the storyboard.

0
votes

you can do it by rearranging constraints along with device rotation

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
coordinator.animate(alongsideTransition: { (_) in

        //code

     }) { (_) in       
   }
}