Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Above UISearchBar

For quite some time, I had been trying to no avail to remove a pernicious line above my UISearchBar. It appears dark on a light background or light on a dark background:

enter image description hereenter image description here

I looked at this question, but it had nothing to do with the separator or pull-to-refresh. When I finally decided to give up and ask for help, I started poking around again and found a blindingly simple (though perhaps not entirely intuitive) solution, so I thought I'd share it in case others are facing the same issue. I configure the search bar as follows:

// I tried this drawing method
[self.searchBar setBackgroundImage:[[UIImage imageNamed:@"linen_bg_bar.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];
// and this one.
[self.searchBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"linen_bg_bar.png"]]];
// Same problem either way.

[self.searchBar setPlaceholder:NSLocalizedString(@"Filter", @"placeholder text in the search bar")];
[self.searchBar setSearchFieldBackgroundImage:[[UIImage imageNamed:@"linen_textfield_search.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(16, 16, 16, 16)] forState:UIControlStateNormal];
[self.searchBar setImage:[UIImage imageNamed:@"linen_icon_search.png"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
self.table.tableHeaderView = self.searchBar;
[self.table setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"linen_bg_dark.png"]]];

Per this post's suggestion, I tried drawing on top of it, but it seemed the drawing still got rendered below the line.

CGRect rect = self.searchBar.frame;
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, rect.size.width, 2)];
lineView.backgroundColor = [UIColor redColor];

See the example with my red "cover-up" line:

enter image description here

like image 908
enjayem Avatar asked Dec 05 '22 12:12

enjayem


1 Answers

I'm not sure this will work, but it makes sense, based on having to set a negative y value for the cover-up view.

searchBar.clipsToBounds = YES;
like image 121
Undo Avatar answered Dec 26 '22 21:12

Undo