UISearchBar clipped under status bar when added to

2020-06-11 04:01发布

问题:

I want my search bar to draw its background extended upwards below the status bar like this:

This is the corresponding code for the image above:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.searchBar = [[UISearchBar alloc] init];
    self.searchBar.delegate = self;
    [self.view addSubview:self.searchBar];

    self.searchBar.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *views = @{@"v":self.searchBar,
                            @"topLayoutGuide":self.topLayoutGuide};

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide][v]" options:0 metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views:views]];

    [self.searchBar setBackgroundImage:[UIImage imageNamed:@"searchbarBG"] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
}

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

The problem arises when I add the search bar to a UISearchDisplayController by appending the following line at the end of the viewDidLoad method:

self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

This is the result I get:

Note that the image looks clipped. This is different from what it looks like if I change UIBarPositionTopAttached to UIBarPositionTop in the code:

The image being clipped made me think of clipsToBounds and I could get the search bar to display properly by adding this piece of code at the end of viewDidLoad:

for (UIView *subview in self.searchBar.subviews) {
    subview.clipsToBounds = NO;
}

So I guess the UISearchDisplayController is messing with the clipsToBounds property of the UISearchBar background view. Has anyone else struggled with this? Is there a problem with my code?

Update:

Back in October 2013, besides posting this question, I also reported this using the Apple Bug Reporter tool. On January 6, 2015 (waaaay too late) I got a reply from Apple Developer Relations:

There are no plans to address this based on the following:

UISearchDisplayController is no longer supported. The desired functionality should be available with UISearchController and if not (or is behaving incorrectly), please file a new bug.

We are now closing this report.

If you have questions about the resolution, or if this is still a critical issue for you, then please update your bug report with that information.

Please be sure to regularly check new Apple releases for any updates that might affect this issue.

回答1:

I was having the exact same issue, but forcing clipsToBounds to YES for the searchBar, did the trick. No need of auto-layout nor UIBarPositioning protocol methods.



回答2:

You conform to the UIBarPositioningDelegate protocol and upon properly becoming the delegate for your SearchBar, implement the delegate method as such

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