ios 移除 iOS7 中 UISearchBar 的边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19899642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:51:02  来源:igfitidea点击:

Remove Border of UISearchBar in iOS7

iosuisearchbar

提问by iEngineer

I'm trying to remove border of UISearchBar in iOS 7. In iOS 6 it's working fine. I created the UISearchBar programatically. I tried almost every thing from Stack Overflow and Google.

我正在尝试删除 iOS 7 中 UISearchBar 的边框。在 iOS 6 中它工作正常。我以编程方式创建了 UISearchBar。我尝试了几乎所有来自 Stack Overflow 和 Google 的东西。

SearchBar looking right now
SearchBar looking right now

SearchBar 正在寻找
SearchBar 正在寻找

What i want to achieve
What i want to achieve

我想要达到的目标
我想要达到的目标

I tried all these stuffs mentioned below

我尝试了下面提到的所有这些东西

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

and

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;
    }
}  

but still no success.

但仍然没有成功。

采纳答案by iEngineer

I found the solution: set the barTintColorof UISearchBarto clearColor

我找到了解决方案:将barTintColorof设置UISearchBarclearColor

topSearchBar.barTintColor = [UIColor clearColor];

回答by nerowolfe

Set Search Style = minimal in Search Bar properties in IB

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

Or

或者

Swift:
searchBar.searchBarStyle = UISearchBarStyleMinimal;

Swift 3:
searchBar.searchBarStyle = .minimal;

回答by Rich Fox

Setting searchBarStyle to UISearchBarStyleMinimal messed up my color setup, so doing this instead fixed the issue.

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

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

For those looking for this option in Swift 4:

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

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

回答by Alap Anerao

For Swift, these 2 lines are enough:

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

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

And then, apply required color that you want:

然后,应用所需的颜色:

self.search.barTintColor = .red

回答by Hemang

This only works if .borderStyle = UITextBorderStyleLine; .

这仅在以下情况下有效.borderStyle = UITextBorderStyleLine;.



My experiments conclusion,

我的实验结论,

  • If you're on iOS7 and above, and if you'll set, searchBar.barTintColor = [UIColor clearColor];then you'll not able to customise background colour of UISearchBar.

  • if you'll set searchBarStyleto UISearchBarStyleMinimalthen it'll mess the color of UISearchBaras said by @Rich Fox.

  • 如果您使用的是 iOS7 及更高版本,并且您要设置,searchBar.barTintColor = [UIColor clearColor];那么您将无法自定义UISearchBar.

  • 如果您设置searchBarStyle为,UISearchBarStyleMinimal那么它会弄乱UISearchBar@Rich Fox 所说的颜色。

So, [self.searchField setBackgroundImage:[[UIImage alloc]init]];solution to remove border.

所以,[self.searchField setBackgroundImage:[[UIImage alloc]init]];去除边框的解决方案。

Update with sample:

使用示例更新:

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 {

}

will give you:

会给你:

enter image description here

在此处输入图片说明

回答by Kreeble Song

Neither only barTintColor, backgroundImagenor backgroundColoralone were doing it for me, but doing them all together worked for me:

不仅barTintColorbackgroundImage也不backgroundColor单独为我做这件事,但一起做对我有用:

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

回答by Daniil Chuiko

Okay. There are so many answers, but they are too complex. I've found this solution:

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

Swift 3 (4)

斯威夫特 3 (4)

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

Where .primary is

.primary 在哪里

extension UIColor {

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

回答by Kyle KIM

Swift 2.1 in Xcode 7.2, this worked for me.

Xcode 7.2 中的 Swift 2.1,这对我有用。

self.searchController.searchBar.backgroundImage = UIImage()

My full code below.

我的完整代码如下。

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

回答by George Lai

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)]];

The order of code seems does matter. It doesn't work if I set the barStyle before the searchBarStyle.

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

回答by David

- (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

在此处输入图片说明