博客> UISearchBar纯代码 设置UISearchBar背景及里面的textField
UISearchBar纯代码 设置UISearchBar背景及里面的textField
2017-12-13 21:23 评论:0 阅读:348 wangwei1
ios 设置UISearchBar背景及里面的textField

一。设置背景

-(UISearchBar*)isSearchBar{

self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH - 30, 44)];

self.searchBar.delegate= self;

self.searchBar.placeholder = @"搜索";

for (UIView *view in self.searchBar.subviews) {

    // for before iOS7.0

// if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {

// [view removeFromSuperview];

// break; // } // for later iOS7.0(include)

    if ([view isKindOfClass:NSClassFromString(@"UIView")] && view.subviews.count > 0) {

        [[view.subviews objectAtIndex:0] removeFromSuperview];

        break;
    }

}

self.searchBar.barStyle=UIBarStyleDefault;

self.searchBar.backgroundColor = COLOR(242, 242, 242, 1);

UISearchDisplayController *searchVC = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

searchVC.delegate = self;

searchVC.searchResultsDelegate = self;

searchVC.searchResultsDataSource = self;

return self.searchBar;

}

二。 1.设置textField ios7以下

for (UIView *view in self.searchBar.subviews) {

if ([view isKindOfClass: [UITextField class]]) {

    UITextField* textField = (UITextField*)view;

      textField.backgroundColor = [UIColor redColor];

    break;

}

}

2.设置textField ios7及以上

层次结构的子视图中已更改 iOS7,以便您可以使用下面的代码:

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];

for (UIView *view in searchBarSubViews) {

if([view isKindOfClass:[UITextField class]])

{

         UITextField* textField = (UITextField*)view;

      textField.backgroundColor = [UIColor redColor];
}

}

  1. list text here
收藏
0
sina weixin mail 回到顶部