On the iPhone 6+ it looks like UINavigationBars have a left margin of 20pt when you add custom left bar button items. On other devices it's 16pt.
That's fine, and on wider screen devices probably helps getting that button within reach of a thumb :)
I'm trying to align my UI below that (it's a table view of menu items) and it's proving tricky.
Is there any way of getting that margin programatically so I can use that value elsewhere in my UI.
Something like this would be ideal:
CGFloat padding = self.navigationBar.leftMargin;
but obviously that doesn't exist :(
Any suggestions?
iOS 8 introduces the concept of layout margins. Each view (UIView
and its subclasses) contains a layoutMargins
property, which will give you exactly what you want.
In addition, you can use layoutMarginsDidChange
to listen to changes to the margin to react on margin changes. This method is called after initial layout as well, so you can react here and layout your view appropriately to the system provided margins. This is preferred, because depending on the device, margins may change when size class changes (iPhone 6 Plus rotation, as an example). This way, your view will always appear using the correct margins.
Read here for more information on layout margins.