user1681673
user1681673

Reputation: 368

Small gap between UINavigationBar and UISearchBar

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

Upvotes: 0

Views: 944

Answers (1)

Byte
Byte

Reputation: 2940

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

Upvotes: 1

Related Questions