iOS remove the gray background frame UISearchBar

When using the UISearchBar I personally feel that a gray background frame and background very dissonant. The results set the background is a colorless, but the setting is over or not. Then I went online looked to see there are two ways to solve.

1. Set barTinColor and the same background that you do not see a gray box. Since the naked eye can not see so this is a cover-up. I do not recommend the use of Kazakhstan. Of course, if you think otherwise trouble with this or can do it.

2. As such it is tricky to do that subviews of the method. This method associated with our version, so the first version was a judge. There subviews iOS7.0 above two layers, a layer or less. It is no good just 7.0 you can directly set the background color and flavor that barTinColor colorless enough. Next, look at the code

There are two ways for writing.

(1)

 float version = [[[UIDevice currentDevice] systemVersion] floatValue];

    if (version == 7.0) {
        _searchBar.backgroundColor = [UIColor clearColor];
        _searchBar.barTintColor = [UIColor clearColor];

    }else{
        for(int i =  0 ;i < _searchBar.subviews.count;i++){
            UIView * backView = _searchBar.subviews[i];
            if ([backView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {
                [backView removeFromSuperview];
                 [_searchBar setBackgroundColor:[UIColor clearColor]];

                break;
            }else{
                NSArray * arr = _searchBar.subviews[i].subviews;
                for(int j = 0;j<arr.count;j++   ){
                    UIView * barView = arr[i];
                    if ([barView isKindOfClass:NSClassFromString(@"UISearchBarBackground")] == YES) {

                        [barView removeFromSuperview];
                         [_searchBar setBackgroundColor:[UIColor clearColor]];

                        break;
                    }
                }
            }
        }
    }
2//     去掉UISearchBar的那个灰色背景框
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    if ([self.searchController.searchBar respondsToSelector:@selector(barTintColor)]) {
        float iosversion7_1 = 7.1;
        if (version >= iosversion7_1){

            [[[[self.searchController.searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];
            [self.searchController.searchBar setBackgroundColor:[UIColor clearColor]];

        }else {            //iOS7.0

            [self.searchController.searchBar setBarTintColor:[UIColor clearColor]];
            [self.searchController.searchBar setBackgroundColor:[UIColor clearColor]];
        }
    }else {
        //iOS7.0以下
        [[self.searchController.searchBar.subviews objectAtIndex:0] removeFromSuperview];

        [self.searchController.searchBar setBackgroundColor:[UIColor clearColor]];
    }

    //    禁止搜索栏上移
    self.searchController.hidesNavigationBarDuringPresentation = NO;
Published 83 original articles · won praise 12 · views 180 000 +

Guess you like

Origin blog.csdn.net/shengdaVolleyball/article/details/61210887