IOS background color UISearchBar In iOS 9

I was looking for a while for this problem, I want my search bar to appear as the BBC News App SearchBar at BBC News APP

I am trying to use all the related methods

  for view in searchBar.subviews {
        if view.isKindOfClass(NSClassFromString("UISearchBarBackground")!) {
            view.removeFromSuperview()
            break;
        }
  }

    self.searchBar.tintColor = UIColor.clearColor()
    self.searchBar.backgroundColor = UIColor.clearColor()
    self.searchBar.translucent = true

here is my conclusion enter image description here

Am I missing something ??? Please help me, thanks!

+10
source share
4 answers

In general, I solve the issue by setting the background image to 'nil' , which is a nonexistent image in my application

enter image description here

my final conclusion enter image description here

===================== Update the final decision ====================

After reading additional documents. I finally found the best solution,

for subView in searchBar.subviews {
        for view in subView.subviews {
            if view.isKindOfClass(NSClassFromString("UINavigationButton")!) {
                let cancelButton = view as! UIButton
                cancelButton.setTitle("取消", forState: UIControlState.Normal)
                cancelButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
            }
            if view.isKindOfClass(NSClassFromString("UISearchBarBackground")!) {
                let imageView = view as! UIImageView
                imageView.removeFromSuperview()
            }
        }
    }

==================== Swift4 =================== =

for subView in searchBar.subviews {
        for view in subView.subviews {
            if view.isKind(of: NSClassFromString("UINavigationButton")!) {
                let cancelButton = view as! UIButton
                cancelButton.setTitleColor(.white, for: .normal)
                cancelButton.setTitle("取消", for: .normal)
            }
            if view.isKind(of: NSClassFromString("UISearchBarBackground")!) {
                let imageView = view as! UIImageView
                imageView.removeFromSuperview()
            }
        }
    }
+10

Swift 3

, backgroundImage :

searchBar.backgroundImage = UIImage()

, barTintcolor:

searchBar.barTintColor = .green
+23

extension UISearchBar {
    func removeBackgroundImageView(){
        if let view:UIView = self.subviews.first {
            for curr in view.subviews {
                guard let searchBarBackgroundClass = NSClassFromString("UISearchBarBackground") else {
                    return
                }
                if curr.isKind(of:searchBarBackgroundClass){
                    if let imageView = curr as? UIImageView{
                        imageView.removeFromSuperview()
                        break
                    }
                }
            }
        }
    }
}
+5

iOS 13:

Terminating app due to uncaught exception 'NSGenericException', reason:
'Missing or detached view for search bar layout. The application must not remove
<UISearchBarBackground: 0x102d05050; frame = (0 0; 414 56); alpha = 0; hidden = YES;
userInteractionEnabled = NO; layer = <CALayer: 0x280287420>> from the hierarchy.'

iOS 9 iOS 13, .

, :

extension UIView {
/// Find the first subview of the specified class.
/// - Parameter className: The class name to search for.
/// - Parameter usingRecursion: True if the search should continue through the subview tree until a match is found; false otherwise
/// - Returns: The first child UIView of the specified class
func findSubview(withClassName className: String, usingRecursion: Bool) -> UIView? {
    // If we can convert the class name until a class, we look for a match in the subviews of our current view
    if let reflectedClass = NSClassFromString(className) {
        for subview in self.subviews {
            if subview.isKind(of: reflectedClass) {
                return subview
            }
        }
    }

    // If recursion was specified, we'll continue into all subviews until a view is found
    if usingRecursion {
        for subview in self.subviews {
            if let tempView = subview.findSubview(withClassName: className, usingRecursion: usingRecursion) {
                return tempView
            }
        }
    }

    // If we haven't returned yet, there was no match
    return nil
}

}

, , . backgroundColorView - , , .

    // On iOS 9, there is still an image behind the search bar. We want to remove it.
    if let backgroundView = searchBar.findSubview(withClassName: "UISearchBarBackground", usingRecursion: true) {
        backgroundView.alpha = 0
        backgroundView.removeFromSuperview()
    }

    // The color on iOS 9 is white. This mimics the newer appearance of the post-iOS 9
    // search controllers
    if let backgroundColorView = searchBar.findSubview(withClassName: "_UISearchBarSearchFieldBackgroundView", usingRecursion: true) as? UIImageView {
        backgroundColorView.backgroundColor = UIColor.lightGray
        backgroundColorView.layer.cornerRadius = 8
        backgroundColorView.alpha = 0.3
        backgroundColorView.image = nil
    }
0
source

All Articles