UISearchBar overlaps status bar in iOS

我的未来我决定 提交于 2019-11-30 04:49:16
Sunil Adhyaru

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

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

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.

malkoty

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;
    }

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!