0

In my navigation bar, I have a magnifying glass icon that brings up a search bar. I'm not using a UISearchDisplayController, so I opted to build my own UINavigationItem and then push it over the standard UINavigationItem using pushNavigationItem.

The problem is that the UINavigationItem seems to be pushed around 8 pixels to the right. This causes the cancel button (with localized text 'Annuleren') to be really close to the edge of the screen.

The green square marks the space

I tried inspecting the self.mySearchBar.bounds at runtime, but the origin is 0,0. I've played around a bit with AutoLayout and programmatically added constraints, but I haven't been successful. I hope it's possible without AutoLayout.

This is my code:

- (IBAction)displaySearchBar:(id)sender {
if (!self.mySearchNavigationItem)
{
    self.mySearchNavigationItem = [[UINavigationItem alloc] initWithTitle:@""];
    self.mySearchNavigationItem.hidesBackButton = YES;
    self.mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
    self.mySearchBar.showsCancelButton = YES;
    self.mySearchBar.delegate = self;
    [self.mySearchBar sizeToFit];
    [self.mySearchBar setPlaceholder:@"Zoeken..."];

    UIView *barWrapper = [[UIView alloc]initWithFrame:self.mySearchBar.bounds];
    [barWrapper addSubview:self.mySearchBar];
    self.mySearchNavigationItem.leftBarButtonItem = nil;
    self.mySearchNavigationItem.backBarButtonItem = nil;

    self.mySearchNavigationItem.titleView = barWrapper;
    UIButton *cancelButton;
    UIView *topView = self.mySearchBar.subviews[0];
    for (UIView *subView in topView.subviews) {
        if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
            cancelButton = (UIButton*)subView;
        }
    }
    if (cancelButton) {
        [cancelButton setTitle:@"Annuleren" forState:UIControlStateNormal];
    }            
}
[self.navigationController.navigationBar pushNavigationItem:self.mySearchNavigationItem animated:YES];

NSTimeInterval delay;
if (self.tableView.contentOffset.y >1000) delay = 0.4;
else delay = 0.1;
[self performSelector:@selector(activateSearch) withObject:nil afterDelay:delay];        

}

Benny G
  • 101
  • 7

2 Answers2

0

try:

self.navigationController.navigationBar.barTintColor = self.mySearchBar.barTintColor;

if that doesn't work, you can add an underlay view to the navigation controller that is the color you would like. this may be useful: Get the right color in iOS7 translucent navigation bar

Community
  • 1
  • 1
Matt J
  • 58
  • 5
  • Thanks for your help. It doesn't change anything, but I see what you're trying to achieve. Using self.mySearchBar.searchBarStyle = UISearchBarStyleMinimal; does that more or less as well, it remove the grey background. The problem with this is that my cancel button ('Annuleren') is still really close to the right edge of the screen. I'm hoping to find a solution that removes the 8 pixels space between the left side of the screen and the search bar. – Benny G Dec 03 '13 at 22:28
  • Ahh, well i have one more suggestion for something I did in the past to get rid of the extra space. You can add a UIView over the navigationBar and add your searchBar and button in the UIView. I can post an example later if needed. – Matt J Dec 03 '13 at 23:35
0

After searching for many hours, I gave up and went for a dirty fix. I'll leave it open for a while, in case someone knows why my searchbar is moved 8 pixels to the right.

Right before showing the UINavigationItem, I move the whole UINavigationBar to x-coordinate -8.

self.navigationController.navigationBar.frame = CGRectMake(-8.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
[self.navigationController.navigationBar pushNavigationItem:self.mySearchNavigationItem animated:YES];

And then on the cancel button click, I move it back to x-coordinate 0.

- (IBAction)cancelSearchBar:(id)sender {
    [self.navigationController.navigationBar popNavigationItemAnimated:YES];    
    self.navigationController.navigationBar.frame = CGRectMake(0.0, self.navigationController.navigationBar.frame.origin.y, self.navigationController.navigationBar.frame.size.width, self.navigationController.navigationBar.frame.size.height);
}
Benny G
  • 101
  • 7