1

I have an app where the UINavigationBar has not adapted to iOS7 positioning and I'm stuck on how to get it to position correctly.

I'm not using Auto Layout but have attached a screenshot of the issue I'm getting:

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Omar
  • 979
  • 3
  • 16
  • 26

4 Answers4

1

In your view controller implement positionForBar: method

- (UIBarPosition)positionForBar:(id <UIBarPositioning>)bar {
    return UIBarPositionTopAttached;
}

Make self as toolbar delegate and maybe you have to move your toolbar down by 20 pt.

Greg
  • 25,317
  • 6
  • 53
  • 62
  • How do I do the second part? Make self as toolbar delegate and maybe you have to move your toolbar down by 20 pt. Appreciate your help – Omar Dec 06 '13 at 17:32
1

The new stuff on iOS7 uses a full screen layout, if you want to put your navigation bar on top you need to implement the UIBarPositioningDelegate in this way:

-(UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
    if ([bar isKindOfClass:[UINavigationBar class]]) {
        return UIBarPositionTopAttached;
    }
    return UIBarPositionAny;
}

Don't forget to assign the delegate of your navigation bar to the class that implements this method, i.e.

//self should be the controller that implements the above method.
myNavigationBarReference.delegate = self; 

P.S. UINavigationBarDelegate, UIToolBarDelegate inherit the UIBarPositioningDelegate.

D33pN16h7
  • 2,030
  • 16
  • 20
  • I added that code to the .m file. Is that correct? Or should it be added to the app delegate? Im not sure how to ssign the delegate of your navigation bar to the class that implements this method. This is one of the first apps I'm developing so apologies if it's basic stuff. Appreciate your help – Omar Dec 06 '13 at 17:30
  • You will need a reference to the NavigationBar, so in order to assign delegate do the following: `myNavigationBarReference.delegate = self', I have edited the answer, hope helps. – D33pN16h7 Dec 06 '13 at 17:31
  • So my code now looks something like this. `-(UIBarPosition)positionForBar:(id)bar { if ([bar isKindOfClass:[UINavigationBar class]]) { return UIBarPositionTopAttached; } return UIBarPositionAny; navbar.delegate=self; } ` – Omar Dec 06 '13 at 17:37
  • However, it's still not positioning it correctly – Omar Dec 06 '13 at 17:37
  • 1
    OMG!, put please navbar.delegate=self; on the viewDidLoad method. – D33pN16h7 Dec 06 '13 at 17:38
  • I have added it there but it made no difference – Omar Dec 06 '13 at 17:44
0

Try to add the following code in viewDidLoad: method

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

May it solve your issue. Happy coding!!

Sahil Mahajan
  • 3,922
  • 2
  • 29
  • 43
0

You can adopt Auto Layout incrementally programmatically just where it is needed. And do you add a navigationBar to your view instead of using navigationController? If so, you can add several lines to solve your problem.

id topGuide = self.topLayoutGuide;
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings (topGuide, yourNavigationBar);
NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[topGuide]-0-[yourNavigationBar]"
                                                               options:0
                                                               metrics:nil
                                                                 views:viewsDictionary];
[self.view addConstraints:constraints];

And if not the case, please let me know your implementation.