UISearchBar overlaps status bar in iOS

前端 未结 6 1352
抹茶落季
抹茶落季 2020-12-29 08:03

I (just like everyone else here) am running into the same Status Bar overlap issue that everyone else is, with a little twist, and that is why I opening a new question about

相关标签:
6条回答
  • 2020-12-29 08:10

    If anyone wants to solve this problem via AutoLayout, instead of adding a constraint searchBar.top = self.view.top add a constraint to the topLayoutGuide, like so:

    searchBar.top = topLayoutGuide.bottom
    
    0 讨论(0)
  • 2020-12-29 08:16

    Struggled around with this issue too. You need to put this into your ViewControllers viewDidLoad method.

    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    

    This should fix it. It's stated in the iOS 7 Transition Guide, too. https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/AppearanceCustomization.html#//apple_ref/doc/uid/TP40013174-CH15-SW1

    0 讨论(0)
  • 2020-12-29 08:16

    I'm not really clear on the bit about the search bar adjusting its own height - my search bars don't do that? But.

    You can add a constraint to the Top Layout Guide in IB, for the search bar. This will cause it to stick to the bottom of the status bar in both ios 6 and ios 7. If you then remove the code where you manually adjust the height of the status bar. That should take care of it. You can see an example of how to do that here: https://developer.apple.com/library/ios/qa/qa1797/_index.html

    0 讨论(0)
  • 2020-12-29 08:19

    I got the exact same problem and based on suggestions like @Ben's and here I ended up adding the following code which helped me:

    - (void) viewDidLayoutSubviews
    
        {
            if(floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
            {
                CGRect viewBounds = self.view.bounds;
                CGFloat topBarOffset = self.topLayoutGuide.length;
                viewBounds.origin.y = topBarOffset * -1;
                self.view.bounds = viewBounds;
    
                self.edgesForExtendedLayout = UIRectEdgeNone;
            }
        }
    

    alternatively this code also works

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
    
            self.automaticallyAdjustsScrollViewInsets=YES;
            self.edgesForExtendedLayout = UIRectEdgeNone;
            self.navigationController.view.backgroundColor = [UIColor whiteColor];
            self.navigationController.navigationBar.translucent = YES;
        }
    
    0 讨论(0)
  • 2020-12-29 08:22

    If the navigationBar is visible do the following in ViewDidLoad :

    self.edgesForExtendedLayout = UIRectEdgeNone;
    

    If the navigationBar is hidden do the following in ViewDidLoad :

    Adjust all the UIView elements by shifting them 20pt

    0 讨论(0)
  • 2020-12-29 08:31

    UISearchBar has a delegate method for what you want. Just place it 20px from top.

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

    Unfortunately it doesn't work together with a UISearchDisplayController.

    0 讨论(0)
提交回复
热议问题