在iOS7中删除UISearchBar的边框
我试图在iOS 7中删除UISearchBar的边框。在iOS 6中,它工作正常。 我以编程方式创build了UISearchBar。 我几乎尝试了Stack Overflow和Google的所有东西。
SearchBar正在寻找
我想要达到什么
我尝试了下面提到的所有这些东西
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; } }
但仍然没有成功。
在IB的search栏属性中设置search样式=最小值
要么
Swift: searchBar.searchBarStyle = UISearchBarStyleMinimal; Swift 3: searchBar.searchBarStyle = .minimal;
设置searchBarStyle到UISearchBarStyleMinimal搞乱我的颜色设置,所以这样做,而不是修复问题。
[self.searchField setBackgroundImage:[[UIImage alloc]init]];
好吧,我通过设置UISearchBar
的barTintColor
为clearColor
find了解决scheme
topSearchBar.barTintColor = [UIColor clearColor];
希望这会帮助其他人。
对于迅速这两条线就足够了:
self.search.translucent = false self.search.backgroundImage = UIImage()
然后应用所需的颜色,你想要的:
self.search.barTintColor = UIColor.redColor()
这只适用于
.borderStyle = UITextBorderStyleLine
; 。
我的实验结论,
-
如果你在iOS7及以上版本,并且设置了
searchBar.barTintColor = [UIColor clearColor];
那么你将无法自定义UISearchBar
背景颜色。 -
如果你将
searchBarStyle
设置为UISearchBarStyleMinimal
那么就像@Rich Fox所说的那样,将会弄乱UISearchBar
的颜色。
所以, [self.searchField setBackgroundImage:[[UIImage alloc]init]];
解决scheme删除边界。
更新样本:
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 { }
会给你:
Swift2.1在Xcode7.2中,这为我工作
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
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,它不起作用。
- (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]; }
barTintColor
, backgroundImage
和backgroundColor
本身都不是为我做的,但是一起工作对我来说,
self.searchBar.translucent = NO; self.searchBar.barTintColor = [styleManager currentSearchBarTintColor]; self.searchBar.backgroundImage = [UIImage new]; self.searchBar.backgroundColor = [styleManager currentSearchBarTintColor];
奥基。 答案如此多的地方,但它们要复杂。 我为我find了这个解决scheme:
Swift 3(4)
searchBar.setBackgroundImage(UIImage(), for: .top, barMetrics: .default) searchBar.backgroundColor = .primary
在哪里.primary是
extension UIColor { static var primary:UIColor { return "#5163F4".color } }