0

I added a UISearch programmatically to my app and the UINavigation bar was added in interface builder. When I added the search bar it left a tiny gap between the search bar and the nav bar. Is there a way to merge them together? Thanks.

Screenshot

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0,    self.view.bounds.size.width, 44.0)];
self.searchBar.delegate = self;
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
self.searchBar.showsCancelButton = NO;
[self.view addSubview:self.searchBar];
user1681673
  • 368
  • 1
  • 6
  • 28

1 Answers1

1

If making sure that SearchBar is right under the navigation bar, I would use autolayout like so:

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

id topLayoutGuide = self.topLayoutGuide;
UISearchBar *searchBar = self.searchBar; //updated

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[searchBar]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(searchBar)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[topLayoutGuide][searchBar]" options:0 metrics:0 views:NSDictionaryOfVariableBindings(searchBar, topLayoutGuide)]];

Edit:

I did some searching and found that it is not a gap. It is an image Apple uses to divide things into place. There are many options you can approach

1.) Search and destroy - find the UIImageView, and remove it from your bar.
Remove UIToolbar hairline in iOS 7

2.) Set custom background for your bar

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
Community
  • 1
  • 1
Byte
  • 2,920
  • 3
  • 33
  • 55
  • Thanks. It's giving me an error at constraintsWithVisualFormat. It says searchBar is not a key in the views dictionary. I tried a few other things and I get the same error. Thanks again for the help. – user1681673 Sep 24 '13 at 19:16
  • Try again with the extra line I added. – Byte Sep 24 '13 at 19:42
  • I tried the code with your changes. The code built but the view looked the same as the picture. – user1681673 Sep 24 '13 at 20:32
  • Any other suggestions Byte? – user1681673 Sep 25 '13 at 20:26
  • Not that I am aware off. I cannot reproduce your problem. I would need to see how you implement the whole thing, or at least the important part. I would not recommend a hack but you can also just set the yOffset to be -1 whereby it will close the gap. But again, beware of the hack, it could lead to unintentional consequences. – Byte Sep 25 '13 at 20:29