Position of navigation bar for modal view - iOS7

前端 未结 8 2116
南笙
南笙 2020-11-29 18:08

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

like this

相关标签:
8条回答
  • 2020-11-29 18:49

    Figured out the 3 options for solving this problem.

    Option 1: Resize the Nav 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 the modal view where 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, when you dismiss the modal view, and you want your status bar back. Add this in 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: Embed in Nav Controller

    Select your modal view, just embed that in a Navigation Controller.

    enter image description here

    0 讨论(0)
  • 2020-11-29 18:55

    I turned "Use Autolayout" off and it worked for me.

    0 讨论(0)
  • 2020-11-29 18:56

    In Swift:

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

    You connect the delegate of your NavigationBar to your view controller and conform to that protocol and by calling 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.

    0 讨论(0)
  • 2020-11-29 18:56

    isTranslucent is set by default to true. Setting it to false will extend the navigation bar below the status bar.

    navigationBar.isTranslucent = false

    0 讨论(0)
  • 2020-11-29 19:00

    After a few tries to move Navigation Bar few pixels down in iOS 7, this is what finally worked for 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 adjusted the Status Bar color to better match my content:

    -(UIStatusBarStyle)preferredStatusBarStyle{
        return UIStatusBarStyleLightContent;
    }
    
    0 讨论(0)
  • 2020-11-29 19:03

    For Swift3 use following..

    func position(for bar: UIBarPositioning) -> UIBarPosition{
        return .topAttached;
    }
    
    0 讨论(0)
提交回复
热议问题