2013-03-12 53 views
4

很长一段时间以来,我一直试图无效地删除我的UISearchBar上的有害线。它出现在一个黑暗的背景光背景或光线暗的:线以上UISearchBar

enter image description hereenter image description here

我看着this question,但它没有任何关系与分离器或拉来刷新。当我最终决定放弃并寻求帮助时,我又开始探索,发现了一个非常简单(尽管可能不是完全直观)的解决方案,所以我想我会分享它,以防其他人面临同样的问题。我配置的搜索栏,如下所示:

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

this post's的建议,我想在它上面绘制,但它似乎绘图仍然得到了呈现该线下方。

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

见我的红色的“障眼法”行的例子:

enter image description here

回答

10

我不肯定这会工作,但它是有道理的基础上,不必设置为掩盖视图 y值。

searchBar.clipsToBounds = YES; 
+1

完美的作品!但对我来说,这似乎很奇怪,搜索栏在界限之外划出一条线。 – enjayem 2013-03-13 16:17:22

+0

@enjayem我知道。你是否特意将'clipsToBounds'变成了某个地方? – Undo 2013-03-13 19:18:37

+0

不是。我所做的只是设置自定义背景图像。 – enjayem 2013-03-13 20:38:02

1

事实证明,我所要做的就是调整掩盖线的框架开始1pt 以上搜索栏,我在搜索栏顶部有一条漂亮的1pt灰线。

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

瞧。

+0

仍然看起来不对...... – Undo 2013-03-12 23:19:58

+0

我绝对可以接受其他解决方案。这是我能够想到的最好的。 – enjayem 2013-03-13 01:41:58

+0

我不是说我可以做得更好。我只是说这个解决方案看起来有点乱。 – Undo 2013-03-13 03:35:15

2

希望这有助于:

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

,你可以在你的searchController

1

在我来说,我是在代码中创建一个UISearchBar并将其放置在导航栏的右边找到搜索栏属性酒吧按钮项目。将背景图像设置为空白图像将移除1像素行。

searchBar.backgroundImage = UIImage()