Navigation bar position for modal viewing - iOS7

In the navigation controller, you will automatically get the correct color and position of the navigation bar as expected.

like this

enter image description here

But in view mode, when you drag the navigation bar, you can position it at the top right, which is too close to the carrier / battery information.

enter image description here

So, you can drag it, guess how far it corresponds to the position of the automatically created one, but then you have a color mismatch. I tried changing the status bar settings in IB, it does not matter.

enter image description here

Is there a right way to deal with this, since when creating a modal view they look like an automatically created navigation view.

thank

+38
ios7 view modal-dialog position navigationbar
Sep 11 '13 at 9:17
source share
7 answers

The best way to overcome this in iOS 7 is to comply with the new UIBarPositioningDelegate protocol.

You connect the delegate of your navigation unit to the view controller (set the view controller as a delegate for the navigation panel either through the storyboard or through the code) and combine it with this protocol and implement the method

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

You can remove the top gap in the view controller. You need to place the bar 20 points below the top edge.

+81
Jan 07 '14 at 9:11
source share

Revealed 3 options for solving this problem.

Option 1: resize the navigation bar

 float currentVersion = 7.0; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= currentVersion) { // iOS 7 self.navBar.frame = CGRectMake(self.navBar.frame.origin.x, self.navBar.frame.origin.y, self.navBar.frame.size.width, 64); } 

Option 2: Hide the status bar

For example, in a modal view in which you want to hide the status bar

Add this method

 - (BOOL)prefersStatusBarHidden { return YES; } 

In viewDidLoad add

 float currentVersion = 7.0; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= currentVersion) { [self prefersStatusBarHidden]; [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)]; } else { [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide]; } 

Now that you reject the modal view and you want your status bar to be back. Add this to viewWillAppear

  float currentVersion = 7.0; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= currentVersion) { [self prefersStatusBarHidden]; [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)]; NSLog(@"ios7"); } else { [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide]; } 

and this but return NO this time

 - (BOOL)prefersStatusBarHidden { return NO; } 

Option 3: Insert into Nav Controller

Select your modal view, just insert it into the navigation controller.

enter image description here

+26
Sep 12 '13 at 0:58
source share

In Swift :

The best way to overcome this in iOS 8.1 and Swift is to comply with the new UIBarPositioningDelegate protocol.

You connect the delegate of your navigation block to the view controller and comply with this protocol and call the method:

 func positionForBar(bar: UIBarPositioning) -> UIBarPosition { return UIBarPosition.TopAttached } 

You can remove the top gap in the view controller. You need to place the bar 20 points below the top edge.

+5
Dec 04 '14 at 17:21
source share

For Swift3, use the following command.

 func position(for bar: UIBarPositioning) -> UIBarPosition{ return .topAttached; } 
+2
Sep 15 '16 at 2:34
source share

After several attempts to move the navigation bar a few pixels in iOS 7, this is what finally helped me:

 -(void)viewWillLayoutSubviews { float iosVersion = 7.0; if ([[[UIDevice currentDevice] systemVersion] floatValue] >= iosVersion) { // iOS 7+ CGRect viewFrame = self.view.frame; viewFrame.origin.y += 10; self.view.frame = viewFrame; } } 

I also tweaked the color of the status bar to better match my content:

 -(UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent; } 
0
May 13 '14 at
source share

I created a navigation controller, deleted the "Root View Controller" that appeared with it. Then hold ctrl and drag the “Navigation Controller” into your view (delete the manually added navigation bar). Set the navigation controller as the “Initial View Controller”, and now it works great for me.

0
Jul 28 '14 at 18:14
source share

I turned off “Use Self-timer,” and it worked for me.

-6
Sep 16 '13 at 11:57 on
source share



All Articles