Remove Border of UISearchBar in iOS7

前端 未结 10 1651
死守一世寂寞
死守一世寂寞 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:03

    Swift 2.1 in Xcode 7.2, this worked for me.

    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
    
    0 讨论(0)
  • 2020-12-24 11:06

    Neither only barTintColor, backgroundImage nor backgroundColor alone were doing it for me, but doing them all together worked for me:

    self.searchBar.translucent      = NO;
    self.searchBar.barTintColor     = [styleManager currentSearchBarTintColor];
    self.searchBar.backgroundImage  = [UIImage new];
    self.searchBar.backgroundColor  = [styleManager currentSearchBarTintColor];
    
    0 讨论(0)
  • 2020-12-24 11:09

    Set Search Style = minimal in Search Bar properties in IB

    Or

    Swift:
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    
    Swift 3:
    searchBar.searchBarStyle = .minimal;
    
    0 讨论(0)
  • 2020-12-24 11:11

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

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

    For those looking for this option in Swift 4:

    searchField.setBackgroundImage(UIImage(), for: .any, barMetrics: UIBarMetrics.default)
    
    0 讨论(0)
  • 2020-12-24 11:11

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

    Swift 3 (4)

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

    Where .primary is

    extension UIColor {
    
        static var primary:UIColor {
            return "#5163F4".color
        }
    }
    
    0 讨论(0)
  • 2020-12-24 11:17
    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.

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