ios-在iOS7中删除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];    
     }
} 

and

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

但仍然没有成功。

iEngineer asked 2020-02-21T20:40:29Z
10个解决方案
92 votes

在IB的搜索栏属性中设置搜索样式=最少

要么

Swift:
searchBar.searchBarStyle = UISearchBarStyleMinimal;

Swift 3:
searchBar.searchBarStyle = .minimal;
nerowolfe answered 2020-02-21T20:40:44Z
56 votes

将searchBarStyle设置为UISearchBarStyleMinimal弄乱了我的颜色设置,因此这样做解决了该问题。

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

对于在Swift 4中寻找此选项的用户:

searchField.setBackgroundImage(UIImage(), for: .any, barMetrics: UIBarMetrics.default)
Rich Fox answered 2020-02-21T20:41:08Z
26 votes

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

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

然后,应用所需的所需颜色:

self.search.barTintColor = UIColor.redColor()
Alap Anerao answered 2020-02-21T20:41:33Z
24 votes

我找到了解决方案:将UISearchBarbarTintColor设置为clearColor

topSearchBar.barTintColor = [UIColor clearColor];
iEngineer answered 2020-02-21T20:41:52Z
11 votes

仅在[self.searchField setBackgroundImage:[[UIImage alloc]init]];时有效。   。


我的实验结论

  • 如果您使用的是iOS7及更高版本,并且要设置[self.searchField setBackgroundImage:[[UIImage alloc]init]];,则您将无法自定义UISearchBarStyleMinimal的背景颜色。

  • 如果您将[self.searchField setBackgroundImage:[[UIImage alloc]init]];设置为2757839233644758058017,则@Rich Fox会说它会使2757839233644758058018的颜色混乱。

因此,采用[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

Hemang answered 2020-02-21T20:42:39Z
3 votes

不仅barTintColorbackgroundImagebackgroundColor都没有为我做这件事,但是一起做对我都有用:

self.searchBar.translucent      = NO;
self.searchBar.barTintColor     = [styleManager currentSearchBarTintColor];
self.searchBar.backgroundImage  = [UIImage new];
self.searchBar.backgroundColor  = [styleManager currentSearchBarTintColor];
Kreeble Song answered 2020-02-21T20:42:59Z
1 votes
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)]];

代码的顺序似乎很重要。 如果将barStyle设置在searchBarStyle之前,则不起作用。

George Lai answered 2020-02-21T20:43:19Z
1 votes

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
Kyle KIM answered 2020-02-21T20:43:43Z
1 votes

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

迅捷3(4)

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

.primary在哪里

extension UIColor {

    static var primary:UIColor {
        return "#5163F4".color
    }
}
Daniil Chuiko answered 2020-02-21T20:44:11Z
0 votes
- (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

David answered 2020-02-21T20:44:26Z
translate from https://stackoverflow.com:/questions/19899642/remove-border-of-uisearchbar-in-ios7