Make UISearchBar background clear

后端 未结 17 1124
野的像风
野的像风 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:37

    Removing the UISearchBarBackground may result in unknown behavior particularly if as you begin to use the element in more advanced ways.

    Ruozi's answer was my preferred solution for grabbing the UISearchBarBackground element however I would suggest setting the alpha to 0 rather than removing the view with the following:

    for (UIView *subview in [[self.searchBar.subviews lastObject] subviews]) {
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview setAlpha:0.0];
            break;
        }
    }
    

    Add the above code to your UIViewController subclass that contains an IBOutlet *searchBar. This code snippet will acheive a transparent background for both iOS8 and iOS9.

    In addition it may be better design decision to include this code in your UISearchBar subclass in order to avoid cluttering your viewController.

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

    My solution working on Swift 4.1 | iOS 11.4 where I change the background and text colors of UISearchBar

    The key is to find the UISearchBar's UITextField component and to change its parameters:

    let searchTextField = searchBar.value(forKey: "searchField") as? UITextField
    searchTextField?.textColor = UIColor.white
    searchTextField?.backgroundColor = UIColor.red
    searchTextField?.attributedPlaceholder =  NSAttributedString(string: "Your Placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white.withAlphaComponent(0.5)])
    

    Then I set the "search" and the "clear" image

    //search image
    searchBar.setImage(UIImage(named: "icon_search"), for: UISearchBarIcon.search, state: .normal)
    //clear image
    searchBar.setImage(UIImage(named: "icon_search_clear"), for: UISearchBarIcon.clear, state: .normal)
    
    0 讨论(0)
  • 2021-01-07 17:40

    UISearchBar includes two subviews,they are 'UISearchBarBackground' and 'UITextField'. In IOS8, you need to remove 'UISearchBarBackground' to clear it.

    for (UIView *subview in [[yourSearchBar.subviews lastObject] subviews]) {        
        if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
            [subview removeFromSuperview];
            break;
        }
    }
    
    0 讨论(0)
  • 2021-01-07 17:40
    extension UISearchBar {
        func changeSearchBarColor(fieldColor: UIColor, backColor: UIColor, borderColor: UIColor?) {
            UIGraphicsBeginImageContext(bounds.size)
            backColor.setFill()
            UIBezierPath(rect: bounds).fill()
            setBackgroundImage(UIGraphicsGetImageFromCurrentImageContext()!, for: UIBarPosition.any, barMetrics: .default)
    
            let newBounds = bounds.insetBy(dx: 0, dy: 8)
            fieldColor.setFill()
            let path = UIBezierPath(roundedRect: newBounds, cornerRadius: newBounds.height / 2)
    
            if let borderColor = borderColor {
                borderColor.setStroke()
                path.lineWidth = 1 / UIScreen.main.scale
                path.stroke()
            }
    
            path.fill()
            setSearchFieldBackgroundImage(UIGraphicsGetImageFromCurrentImageContext()!, for: UIControlState.normal)
    
            UIGraphicsEndImageContext()
        }
    }
    
    0 讨论(0)
  • 2021-01-07 17:40

    For iOS 12, swift 4.x A bit too hacky but Debug view hierarchy shows _UISearchBarSearchFieldBackgroundView is responsible to the background color. To get to it, ...

    for subview in searchBar.subviews.last!.subviews {
       if subview.isKind(of: NSClassFromString("UISearchBarTextField")!) {
           for v in subview.subviews {
              if v.isKind(of: NSClassFromString("_UISearchBarSearchFieldBackgroundView")!) {
                      v.removeFromSuperview()
                    }
                }
            }
        }
    

    This is all too hacky. I prefer asking designer to accept Apple default color.

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

    Simply use searchBar.searchBarStyle = .minimal works for me.

    I had used many workarounds for a long time, until i discover this property accidentally, and works like a charm.

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