Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 UISearchBar right spacing

Don't know why, my search bar in iOS 7 is leaving a right space. It's ok in iOS 6.

I know it has something to do with the section index, because if I remove it the space disappears, but I don't know how to fix it. Any thoughts?

enter image description here

like image 531
Odrakir Avatar asked Oct 03 '13 09:10

Odrakir


4 Answers

Embed your UISearchBar in a UIView and then add that as the tableHeaderView. Structuring it that way in a storyboard worked for me. I'm guessing iOS resizes the UISearchBar in the tableHeaderView, but leaves a basic UIView alone (and doesn't bother to look inside it).

You might also want to make the section index transparent, which I did with:

[[UITableView appearance] setSectionIndexBackgroundColor:[UIColor clearColor]];
[[UITableView appearance] setSectionIndexTrackingBackgroundColor:[UIColor clearColor]];
like image 50
Chad Podoski Avatar answered Oct 23 '22 00:10

Chad Podoski


Until a better answer appears, I just manually changed the frame of the search bar like this:

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];        

    CGRect barFrame = self.searchBar.frame;
    barFrame.size.width = self.view.bounds.size.width;
    self.searchBar.frame = barFrame;

}
like image 39
Odrakir Avatar answered Oct 23 '22 00:10

Odrakir


I had this same issue with the iPhone 6/ 6Plus when using a SearchDisplayController. (Using Swift)

I tried setting the frame of the search bar but with no luck but i noticed that if i tapped on the textField of the UISearchBar and then cancelled it then it would take on the proper size of the view. I therefore managed to fix the issue by calling the code below in ViewDidLoad of the viewController using the search.

self.searchController.setActive(true, animated: false)
self.searchController.setActive(false, animated: false)
like image 4
Swinny89 Avatar answered Oct 23 '22 02:10

Swinny89


self.contactsTableView.sectionIndexBackgroundColor = [UIColor clearColor];

The reason for that white edge is because your index layer has a white background and is on top of the search bar. This should be sufficient.

like image 3
Michael Shang Avatar answered Oct 23 '22 00:10

Michael Shang