我的问题已在标题中指明:我想摆脱UISearchBar
底部绘制的黑线。有什么想法吗?
这是我的意思的图像:
更新:
我认为该行是UITableView
的{{1}}的一部分。我仍然不知道如何删除它。
答案 0 :(得分:72)
试试这个
searchBar.layer.borderWidth = 1;
searchBar.layer.borderColor = [[UIColor lightGrayColor] CGColor];
答案 1 :(得分:43)
所以,我已经挖掘了API试图弄清楚为什么会这样。显然,UISearchBar
API编写的任何人都会将线光栅化到图像上并将其设置为backgroundImage
。
我建议一个更简单的解决方案,如果你想设置backgroundColor
并摆脱细线:
searchBar.backgroundColor = <#... some color #>
searchBar.backgroundImage = [UIImage new];
或者如果你只需要一张没有细线的背景图片:
searchBar.backgroundImage = <#... some image #>
答案 2 :(得分:26)
我0.5px
的顶部和底部都有UISearchBar
条黑色水平线。到目前为止,摆脱它们的唯一方法是将其样式设置为Minimal
:
mySearchBar.searchBarStyle = UISearchBarStyleMinimal;
答案 3 :(得分:19)
我通过向subview
的视图堆栈添加searchBar
来修复此问题,如下所示:
CGRect rect = self.searchBar.frame;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, rect.size.height-2,rect.size.width, 2)];
lineView.backgroundColor = [UIColor whiteColor];
[self.searchBar addSubview:lineView];
这里,self.searchBar
是我的控制器类的UISearchBar
指针。
答案 4 :(得分:6)
答案 5 :(得分:5)
Swift 4.2:
controller.searchBar.layer.borderWidth = 1
controller.searchBar.layer.borderColor = UIColor(red: 255/255, green: 253/255, blue: 247/255, alpha: 1.0).cgColor
根据Ayush的回答回答。
答案 6 :(得分:3)
在将nil
放在那里之前,将tableHeaderView设置为UISearchBar
。
如果这没有帮助,请尝试掩盖它。首先将搜索栏添加到通用且大小合适的UIView
(例如“包装器”)作为子视图,然后
CGRect frame = wrapper.frame;
CGRect lineFrame = CGRectMake(0,frame.size.height-1,frame.size.width, 1);
UIView *line = [[UIView alloc] initWithFrame:lineFrame];
line.backgroundColor = [UIColor whiteColor]; // or whatever your background is
[wrapper addSubView:line];
[line release];
然后将其添加到tableHeaderView。
self.tableView.tableHeaderView = wrapper;
[wrapper release];
答案 7 :(得分:3)
这个问题已经解决了,但也许我的解决方案可以帮助别人。我有一个类似的问题,除了我试图删除1px顶部边框。
如果您继承UISearchBar
,则可以覆盖此框架。
- (void)setFrame:(CGRect)frame {
frame.origin.y = -1.0f;
[super setFrame:frame];
self.clipsToBounds = YES;
self.searchFieldBackgroundPositionAdjustment = UIOffsetMake(0, 1.0f);
}
或者,如果你想修复底部像素,你可以做这样的事情,(未经测试)。
- (void)setFrame:(CGRect)frame {
frame.origin.y = 1.0f;
[super setFrame:frame];
self.clipsToBounds = YES;
self.searchFieldBackgroundPositionAdjustment = UIOffsetMake(0, -1.0f);
}
仅为了简单起见,clipsToBounds
中有searchFieldBackgroundPositionAdjustment
和setFrame
。
此外,searchFieldBackgroundPositionAdjustment
仅需要重新定位搜索字段。
<强>更新强>
事实证明,tableView
会在searchBar处于活动状态时从更新origin.y
转移1px。感觉有点奇怪。我意识到解决方案就像设置self.clipsToBounds = YES;
答案 8 :(得分:2)
这可笑的是,必须为这条1 px的小线经历圈和边界。我已经搜寻了几个小时才能摆脱它。我尝试了不同答案的组合以使其正常工作。当我回到这里时,我意识到Oxcug已经有了它,但是它在Objective-C中,而这对我来说并不是原生的。
无论如何,这就是 Swift 5 中的答案。如果您想在实际的搜索文本字段中使用彩色背景,我也添加了该背景。
// these 2 lines get rid of the 1 px line
searchBar.backgroundColor = .white
searchBar.backgroundImage = UIImage()
// this line will let you color the searchBar textField where the user actually types
searchBar.searchTextField.backgroundColor = UIColor.lightGray
答案 9 :(得分:1)
警告。这是一个黑客。想知道更好,更官方的方式。
您可以使用pony调试器来确定它在子视图层次结构中的位置。我认为您看到的是一个名为“separator”的私有UIImageView
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
for (UIView* view in self.searchBar.subviews) {
if (view.frame.size.height == 1 && [view isKindOfClass:[UIImageView class]]) {
view.alpha = 0;
break;
}
}
}
答案 10 :(得分:0)
与我合作的是什么,将搜索栏barTintColor
设置为导航栏颜色
searchBar.barTintColor = UIColor.colorWithHexString(hexStr: "479F46")
经过测试,它可以解决iOS 10.x和11.x的问题
GIF:
答案 11 :(得分:0)
我用过
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
searchBar.backgroundImage = UIImage() // Removes the line
它就像一个魅力:)
答案 12 :(得分:-1)
您可以使用
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:@"someImage.png"]forState:UIControlStateNormal];
在iOS 5+上摆脱界限。