Make UISearchBar background clear

后端 未结 17 1123
野的像风
野的像风 2021-01-07 17:17

I am not able to clear search bar I have tried to make it clear by setting its background color clear and I have also placed one image under searchbar

I have also ma

相关标签:
17条回答
  • 2021-01-07 17:51

    For anyone coming here and not being able to make the remove or alpha solution work for you, make sure you have your searchBar SearchStyle NOT on MINIMALIST. Change it to Default and use any of the codes provided here

    0 讨论(0)
  • 2021-01-07 17:52

    If you want to change this...

    To this...

    Use:

    self.searchBar.searchTextField.backgroundColor = .clear
    
    0 讨论(0)
  • 2021-01-07 17:52

    Setting the backgroundColor and barTintColor did not work me iOS 9+, left me with a black background. However Ruozi solution will work. Here is a swift version of the same code.

    for view in _searchBar.subviews.last!.subviews {
        if view.isKindOfClass(NSClassFromString("UISearchBarBackground")!) {
            view.removeFromSuperview()
        }
    }
    
    0 讨论(0)
  • 2021-01-07 17:52

    Subclass the uisearchbar and override initWithFrame and initWithCoder method and set the background color as

    // For searchbar created programmatically
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
        if (self) {
            [self configureSetup];
        }
        return self;
    }
    
    // For search bar created on xib
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self configureSetup];
        }
        return self;
    }
    
    - (void)configureSetup
    {
        self.backgroundColor = [UIColor clearColor];
    }
    

    and use the subclassed searchbar instead in place where you specified UISearchBar

    0 讨论(0)
  • 2021-01-07 17:53

    Oh man! I've been looking everywhere for a solution and trying multiple things. As @gmogames had mentoned above you can use any of these answers, but make sure the Search Bar Style is set to default or it won't work. So, to clarify on the solution this is what worked for me:

    Set Search Bar Style to default in the Storyboard or code(wherever you created the UISearchBar. Then add this code to your view controller:

    mySearchBar.searchTextField.backgroundColor = .clear
    
    0 讨论(0)
提交回复
热议问题