UINavigationBar left edge

On iPhone 6+, it looks like UINavigationBars has a 20-point left margin when you add custom bar items to the left. On other devices it is 16pt.

That's fine and on wider device screens probably helps to get that button within thumb's reach :)

I'm trying to line up my UI below (this is a tabular view of menu items) and this proves the complexity.

Is there a way to get this stock programmatically so that I can use this value elsewhere in my UI.

Something like this would be ideal:

CGFloat padding = self.navigationBar.leftMargin;

      

but obviously it doesn't exist :(

Any suggestions?

+3


source to share


2 answers


IOS 8 introduces the concept of layout layout. Each view ( UIView

and its subclasses) contains a property layoutMargins

that will give you exactly what you want.

In addition, you can use layoutMarginsDidChange

to listen for margin changes to react to margin changes. This method is also called after the initial layout, so you can react here and place your view appropriately on the system provided fields. This is preferable because, depending on the device, margins can change when the size class changes (such as rotating the iPhone 6 Plus). This way, your view will always render using the correct margins.



Read here for more information on layout dimensions.

+5


source


You can get subviews UINavigationBar

.

Then, after you can repeat the loop to find your own view. Using this point of view, you can find its position. How,



self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[Your Custom View Class]];

    for(id object in self.navigationController.navigationBar.subviews){
        if([object isKindOfClass:[Your Custom View Class]]){
            UIView *item = (UIView *)object;
            NSLog(@"%f",item.frame.origin.x);
        }
    }

      

Hope this helps you.

+1


source







All Articles