UISearchBar上方的线

时间:2013-03-12 19:11:59

标签: ios uitableview uisearchbar

很长一段时间以来,我一直试图在我的UISearchBar上面删除一条有害的线路。它在浅色背景上显得很暗或在深色背景上显得很亮:

enter image description here enter 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

4 个答案:

答案 0 :(得分:10)

我不是肯定这会有效,但它有意义,因为必须为掩盖视图设置 y值。

searchBar.clipsToBounds = YES;

答案 1 :(得分:3)

希望这会有所帮助:

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

您可以在searchController中找到searchBar属性

答案 2 :(得分:2)

在我的情况下,我在代码中创建了一个UISearchBar,并将其作为右侧栏按钮项目放在导航栏中。将背景图像设置为空图像会删除1像素线。

searchBar.backgroundImage = UIImage()

答案 3 :(得分: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];

瞧。

相关问题