自定义UISearchBar: 试图去掉搜索栏下面的1像素黑线

57
我的问题已经在标题中说明了:我想去掉UISearchBar底部绘制的黑线。有什么想法吗?
这是我所说的内容的图像: search bar with black bottom line 更新:
我认为这条线是UITableViewtableHeaderView的一部分。 我仍然不知道如何将其删除。

使用此代码来删除1像素边框:https://stackoverflow.com/a/64535982/2692839 - Umair Ali
13个回答

76

试一试这个

 searchBar.layer.borderWidth = 1;

 searchBar.layer.borderColor = [[UIColor lightGrayColor] CGColor];

5
在我的情况下,这对我起作用。我正在使用自定义的搜索栏色调,所以我的代码如下(Swift): searchBar.layer.borderColor = searchBar.barTintColor?.cgColor - Paludis
帮助我理解为什么它有效的是错误地设置了“searchBar.layer.borderColor = searchBar.tintColor?.CGColor”。 - Adam Eberbach
1
所以有人说要添加 searchBar.layer.borderColor = searchBar.barTintColor?.CGColor,而另一个人则说要删除它 -_- - Iulian Onofrei

53

为什么:

所以,我已经深入研究了API,试图弄清楚为什么会发生这种情况。显然,编写UISearchBar API的人将线条光栅化到图像上,并将其设置为其backgroundImage

解决方案:

我提出了一个更简单的解决方案,如果您想设置backgroundColor并摆脱毛刺:

searchBar.backgroundColor = <#... some color #>
searchBar.backgroundImage = [UIImage new];

如果您只需要一个没有发丝的背景图片:

searchBar.backgroundImage = <#... some image #>

1
对于iOS7+,您应该使用setBackgroundImage:forBarPosition:barMetrics:方法。 - tyler
此解决方案移除了该条。感谢@Oxcug! - KamyFC

46

我的UISearchBar顶部和底部都有0.5像素的黑色水平线。到目前为止,我唯一能想到消除它们的方法是将其样式设置为Minimal

mySearchBar.searchBarStyle = UISearchBarStyleMinimal;

4
这会使搜索栏中的文本字段变为灰色。 - Neeraj Joshi

33

解决方法:

XCode 10.1 Swift 4.2

在此输入图片描述


2
太棒了,你让我的一天都变得美好了。 - Gaurav
"programmatically" 的翻译是:以编程方式 - José Raúl Toledano R

22

我通过像下面这样向searchBar的视图堆栈添加一个subview来解决了这个问题:

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指针。

我在一个 UISearchBar 的子类的 layoutSubviews 方法中添加了这行代码。反正我已经在处理子类了。 - strave

6

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的回答,做出以下回答。

4
这些折腾着删除这个1像素线的过程真是荒谬。我已经浏览了几个小时的谷歌搜索结果,尝试了不同答案的组合来解决它。当我回到这里时,我意识到 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

enter image description here


3
在将UISearchBar放置在表头之前,请将tableHeaderView设置为nil
如果这不起作用,尝试遮盖它。首先将搜索栏添加到通用且适当大小的UIView(例如“wrapper”)作为子视图,然后...
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];

谢谢您的解决方案。不幸的是,这两个解决方案都没有涵盖到这一行。也许这是表视图上面的阴影?很奇怪! - strave
在将 UISearchBar 放置在表头视图之前,将 tableHeaderView 设置为 nil。这样做的原因是什么? - Ben Collins

3
这个问题已经解决了,但也许我的解决方案可以帮助其他人。我遇到了类似的问题,只是我试图去掉顶部1像素的边框。
如果你继承 UISearchBar,你可以这样覆盖它的 frame。
- (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);
}

仅为简化示例,clipsToBoundssearchFieldBackgroundPositionAdjustmentsetFrame 中。同时,searchFieldBackgroundPositionAdjustment 只是为了重新居中搜索栏。 更新 原来当搜索栏激活时,tableView 会从更新 origin.y 开始向上移动 1 像素。这感觉有点奇怪。我意识到解决方案就是设置 self.clipsToBounds = YES;

3

我使用了

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var searchBar: UISearchBar!
    


    override func viewDidLoad() {
        super.viewDidLoad()
        searchBar.backgroundImage = UIImage() // Removes the line

它像魔法一样奏效了 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接