Line above the UISearchBar

For some time I tried to remove the harmful line above my UISearchBar. It seems dark against a light background or a light dark background:

enter image description hereenter image description here

I reviewed this question , but it had nothing to do with a separator or pull-to-refresh. When I finally decided to refuse and ask for help, I started picking my tongue again and found a dazzlingly simple (although perhaps not quite intuitive) solution, so I decided to share it if others encounter the same problem. 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"]]]; 

In the expression of this post, I tried to draw on top of it, but it seemed that the drawing was still displayed 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 an example with my red hiding line:

enter image description here

+4
source share
4 answers

I'm not sure , this will work, but it makes sense based on the need to set a negative y for the hidden view.

 searchBar.clipsToBounds = YES; 
+10
source

Hope this helps:

 self.searchBar.layer.borderColor = Color.CGColor; self.searchBar.layer.borderWidth = 1; 

you can find the searchBar property in your search

+2
source

It turns out that all I had to do was adjust the hiding line border to start 1pt above the search bar, and I got a nice gray line on top of my search bar.

 CGRect rect = self.searchBar.frame; UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, -1, rect.size.width, 1)]; lineView.backgroundColor = [UIColor colorWithHexString:@"353535" alpha:1.0]; [self.searchBar addSubview:lineView]; 

Voila.

+1
source

In my case, I created the UISearchBar code and placed it in the navigation panel as an element of a button on the right panel. Setting the background image to a blank image deleted 1 pixel line.

 searchBar.backgroundImage = UIImage() 
+1
source

Source: https://habr.com/ru/post/1468805/


All Articles