在iOS7中删除UISearchBar的边框

时间:2013-11-11 05:44:57

标签: ios uisearchbar

我正在尝试在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 个答案:

答案 0 :(得分:89)

在IB

中的搜索栏属性中设置搜索样式=最小值

或者

Swift:
searchBar.searchBarStyle = UISearchBarStyleMinimal;

Swift 3:
searchBar.searchBarStyle = .minimal;

答案 1 :(得分:55)

将searchBarStyle设置为UISearchBarStyleMinimal会搞砸我的颜色设置,所以这样做会修复问题。

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

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

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

答案 2 :(得分:24)

我找到了解决方案:将barTintColor的{​​{1}}设置为UISearchBar

clearColor

答案 3 :(得分:22)

对于Swift来说,这两行就足够了:

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

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

self.search.barTintColor = UIColor.redColor()

答案 4 :(得分: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 {

}

会给你:

enter image description here

答案 5 :(得分:2)

单独barTintColorbackgroundImagebackgroundColor都没有为我做这件事,但一起做这些对我有用:

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

答案 6 :(得分: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,它就不起作用。

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

答案 8 :(得分:0)

- name: add custom settings 
  lineinfile: dest=/etc/fail2ban/jail.local regexp='^' line='maxretry = 3\nfindtime = 10800\nbantime = 86400' create=yes state=present backrefs=yes

enter image description here

答案 9 :(得分:0)

好。有很多答案,但它们太复杂了。 我找到了这个解决方案:

Swift 3(4)

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

其中.primary是

extension UIColor {

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