iOS7中如何去掉UISearchBar的边框

56

我正在尝试在iOS 7中去除UISearchBar的边框。在iOS 6中,它正常工作。我以编程方式创建了UISearchBar。我尝试了Stack Overflow和Google上的几乎所有方法。

目前的SearchBar外观如下:
SearchBar looking right now

我想要实现的效果是:
What i want to achieve

我尝试了下面提到的所有方法。

searchBar.layer.borderWidth = 1;
searchBar.layer.borderColor = [[UIColor whiteColor] CGColor];

以及

for (id img in searchBar.subviews)
{       
     if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
     {     
            [img removeFromSuperview];    
     }
} 

for (UIView *sub in self.tableView.tableHeaderView.subviews) {
    if ([sub isKindOfClass:[UIImageView class]]) {
        sub.hidden = YES;
    }
}  

但是仍然没有成功。

10个回答

109

在IB中的搜索栏属性中设置搜索样式为minimal。

或者

Swift:
searchBar.searchBarStyle = UISearchBarStyleMinimal;

Swift 3:
searchBar.searchBarStyle = .minimal;

我通过编程创建了 UISearchBar - iEngineer
searchBar.searchBarStyle = UISearchBarStyleMinimal; 搜索栏.searchBarStyle = UISearchBarStyleMinimal; - nerowolfe
1
UISearchBar的searchBarStyle属性可在iOS7.0及以上版本中使用。 - John Paul Manoza
感谢!!!工作正常!!Xamarin.iOS代码searchBar.SearchBarStyle = UISearchBarStyle.Minimal; - Marcos José Pérez Pérez

60

将searchBarStyle设置为UISearchBarStyleMinimal会破坏我的颜色设置,所以改为做这个修复了问题。

[self.searchField setBackgroundImage:[[UIImage alloc]init]];

对于那些在寻找Swift 4中的此选项的人:

searchField.setBackgroundImage(UIImage(), for: .any, barMetrics: UIBarMetrics.default)

6
这个答案比这里的任何其他答案都要好! - Umit Kaya
这是对我有效的一个,我试过这里几乎所有其他的.. Xcode 11.3.1 - Taoufik

29

对于Swift来说,这两行足矣:

self.search.isTranslucent = false
self.search.backgroundImage = UIImage()

然后,应用您想要的所需颜色:

self.search.barTintColor = .red

23
我找到了解决方案:将 UISearchBarbarTintColor设置为clearColor
topSearchBar.barTintColor = [UIColor clearColor];

有时候,我使用相同的代码,但一个可以正常工作,而另一个却显示黑色。因此,我使用whitecolor来解决这个问题。很奇怪。 - chings228
9
每当我设置透明色时,它总是显示为黑色 :( - Ajith Renjala
尝试执行nerowolfe的答案: - TheMan
@Thought-Beast 也许你将 alpha 设置为了一些最小值,比如 0.1 或其他数值? - iEngineer

11

仅当.borderStyle = UITextBorderStyleLine时,此方法才有效;


我的实验结论是:

  • 如果你使用的是iOS7及以上版本,并且设置了searchBar.barTintColor = [UIColor clearColor]; ,那么你将无法自定义UISearchBar的背景颜色。

  • 如果你将searchBarStyle设置为UISearchBarStyleMinimal,那么它会像@ Rich Fox所说的那样破坏UISearchBar的颜色。

因此,[self.searchField setBackgroundImage:[[UIImage alloc]init]];可以解决去除边框的问题。

以下是样例更新:

UISearchBar *search = [[UISearchBar alloc] init];
search.tag = kTagSearchBar;
search.delegate = self;
search.tintColor = [UIColor redColor];
search.searchBarStyle = UISearchBarStyleMinimal;
search.frame = CGRectMake(0, 0, 320, 50);
search.placeholder = @"Search";
search.barTintColor = [UIColor blueColor];
search.translucent = NO;
search.opaque = NO;
search.showsCancelButton = NO;
[search setBackgroundImage:[[UIImage alloc] init]];
[self.view addSubview:search];

//customize textfield inside UISearchBar
@try {
    for (id object in [[[search subviews] firstObject] subviews])
    {
        if (object && [object isKindOfClass:[UITextField class]])
        {
            UITextField *textFieldObject = (UITextField *)object;
            textFieldObject.backgroundColor = [UIColor whiteColor];
            textFieldObject.borderStyle = UITextBorderStyleLine;
            textFieldObject.layer.borderColor = [UIColor blueColor].CGColor;
            textFieldObject.layer.borderWidth = 1.0;
            break;
        }
    }
}
@catch (NSException *exception) {
    NSLog(@"Error while customizing UISearchBar");
}
@finally {

}

会给你:

在此输入图片描述


这是正确的。设置背景图像移除了边框。 - Rishab

4

只使用barTintColorbackgroundImagebackgroundColor都不能满足我的需求,但是同时使用它们可以达到效果:

self.searchBar.translucent      = NO;
self.searchBar.barTintColor     = [styleManager currentSearchBarTintColor];
self.searchBar.backgroundImage  = [UIImage new];
self.searchBar.backgroundColor  = [styleManager currentSearchBarTintColor];

3

好的。有很多答案,但它们太复杂了。

我找到这个解决方案:

Swift 3(4)

searchBar.setBackgroundImage(UIImage(), for: .top, barMetrics: .default)
searchBar.backgroundColor = .primary

.primary是什么

extension UIColor {

    static var primary:UIColor {
        return "#5163F4".color
    }
}

1

在Xcode 7.2中,Swift 2.1对我有效。

self.searchController.searchBar.backgroundImage = UIImage()

我的完整代码如下。
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
self.searchController.searchBar.sizeToFit() 
tableView.sectionIndexBackgroundColor = UIColor(red: 0/255, green: 181/255, blue: 229/255, alpha: 1.0)
self.searchController.searchBar.backgroundColor = UIColor(red: 0/255, green: 181/255, blue: 229/255, alpha: 1.0)
self.searchController.searchBar.barTintColor = UIColor(red: 0/255, green: 181/255, blue: 229/255, alpha: 1.0)
self.searchController.searchBar.backgroundImage = UIImage()
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar

1
self.searchBar.translucent = NO;
self.searchBar.opaque = NO;
if ([self.searchBar respondsToSelector:@selector(setSearchBarStyle:)]) {
    self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}

// iOS 7 remove 1 px bottom border
if ([self.searchBar respondsToSelector:@selector(setBarTintColor:)]) {
    self.searchBar.barTintColor = [UIColor clearColor];
}
self.searchBar.barStyle = UIBarStyleDefault;

// to remove the 1px bottom border iOS 5, 6
[self.searchBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] andSize:CGSizeMake(1.0f, 1.0f)]];

代码的顺序似乎很重要。如果我先设置 searchBarStyle 再设置 barStyle,它就不起作用了。


0
- (void)createNavigationBar
{
    _searchBar = [[UISearchBar alloc]init];
    _searchBar.backgroundColor = [UIColor whiteColor];
    _searchBar.placeholder = @"Search";
    _searchBar.translatesAutoresizingMaskIntoConstraints = NO;

    self.navigationItem.titleView = _searchBar;
    NSDictionary *viewsDictionary = @{@"SearchBar":_searchBar};
    NSArray *constraint_POS_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[SearchBar(30)]|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:viewsDictionary];
    NSArray *constraint_POS_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[SearchBar]-15-|"
                                                                        options:0
                                                                        metrics:nil
                                                                          views:viewsDictionary];
    [_searchBar.superview addConstraints:constraint_POS_V];
    [_searchBar.superview addConstraints:constraint_POS_H];

}

enter image description here


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