Remove Border of UISearchBar in iOS7

前端 未结 10 1652
死守一世寂寞
死守一世寂寞 2020-12-24 10:46

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

相关标签:
10条回答
  • 2020-12-24 11:20

    This only works if .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 searchBarStyle to UISearchBarStyleMinimal then it'll mess the color of UISearchBar as said by @Rich Fox.

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

    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

    0 讨论(0)
  • 2020-12-24 11:21

    I found the solution: set the barTintColor of UISearchBar to clearColor

    topSearchBar.barTintColor = [UIColor clearColor];
    
    0 讨论(0)
  • 2020-12-24 11:25

    For Swift, these 2 lines are enough:

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

    And then, apply required color that you want:

    self.search.barTintColor = .red
    
    0 讨论(0)
  • 2020-12-24 11:28
    - (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];
    
    }
    

    0 讨论(0)
提交回复
热议问题