sizeThatFits not working


Been using sizeThatFits to customise the hight of the navigation bar on iOS. Was working perfectly in the latest version of iOS 10. Doesn't work on the first iOS 11 beta. Was it removed because of the new Navigation Bar in iOS 11, or would we see it come back again. If it is indeed removed for good, what's a good way to resize a custom navigation bar?


I observed the same issue with Beta 1. In the release notes there was mention of some 3rd-party apps not looking right in the Nav Bar area.

In beta 2 there is a note that these apps now look OK.

Unfortunately, I have exactly the same issue as you - using sizeThatFits in custom Nav Bar. It is still not invoked in Beta 2.

I did raise a RADAR for BETA 1 which was not touched since it seemed to be the same issue as the mentioned 3rd Party apps. I am about to modify my RADAR with an Xcode project that demonstrates that when running on iOS10 sim it works as designed, but in iOS11 Beta 2 sizeThatFits is not invoked.

I submitted RADAR 32582673 and the response a few hours ago was "Engineering has determined that this issue behaves as intended"

Very unhelpful. It does not explain why it behaves as intended and what change to make to restore the desired outcome.

Beta 2 release dos state that peviosuly-known issues with UINavigationBar have been resolved. Problem is that I have a released app that when installed onto Beta2 on iPad shows correct nav bar height but when rotated the nav bar collapses. This does not happen on iOS9 or 10.

I received this from Apple Engineering:

"In order for overriding -sizeThatFits: to make sense, UINavigationBar needs to have a contract with which it subsequently lays out its subviews, or a contract for which subclassers can correctly ensure layout of those subviews. UINavigationBar has neither, and as such overriding -sizeThatFits: to return a size other than what UINavigationBar would provide has never been supported.

With iOS 11, sizing requirements for UINavigationBar became sufficiently complex that -sizeThatFits: no longer proves sufficient for UINavigationController’s needs. As such UINavigationController no longer consults -sizeThatFits: for sizing information when your application is linked against iOS 11."

So it's official - even engineering admits that when it comes to iOS layouts, you can't get there from here.

Thanks ghr for the info.

But did they offer a workaround or a new way of achieving the same result?


No, all they said is what I posted.

A possible solution that is out there and which I have now implemented involves not using UINavigationBar at all; hide it. For UITableViewController subclasses I have added a view to tableHeaderView. I have implemented scrollViewDidScroll: method to ensure the header does not move.

I referred to this post:

For UIViewController subclasses one can create a new UIViewControllerClass with a view for the header and below that a container view which holds the UIViewController subclass.

If there are buttons in the tableHeaderView then extra work is needed since as the tableView scrolls under the headerView the cells under the header view receive the touches, not the buttons. The cells should implement, or be a subclass, of a UITableViewCell that implements touchesBegan: and related methods so as to pass the touches to the buttons if the touch is over the button.