Search code examples
iosswiftswift3uisearchbaribdesignable

Change the normal search bar to below image like search bar


Here I Just placed one search bar controller which default design in Xcode. Can any will help me to make search bar likes the second one?

Default

enter image description here

Expected output/UI Design

enter image description here

Here I have attached my own UITextField design it's may help you to solve this problem: Github


Solution

  • I have tried your question by adding UISearchBar to UINavigationBar.

    Customising :

    @IBOutlet weak var BGView: UIView! //CONSTRAINTS top, left and right = 0, height = 64
    @IBOutlet var topSearchBarr: UISearchBar!
    
    override func viewWillAppear(_ animated: Bool) {
    
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = true
        self.navigationController?.view.backgroundColor = .clear
    
    
        let gradient: CAGradientLayer = CAGradientLayer()
        //Set the two colors of your gradient
        gradient.colors = [UIColor.purple.cgColor, UIColor.red.cgColor]
        //Set it's location
        gradient.locations = [0.0 , 1.0]
        gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
        gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
        gradient.frame = CGRect(x: 0.0, y: 0.0, width:   self.view.frame.size.width, height: 64)
    
        BGView.layer.insertSublayer(gradient, at: 0)
        getSearchBarSubViews()
    }
    
    func getSearchBarSubViews()
    {
        let navSubVws = self.navigationController?.navigationBar.subviews
    
        for subVws in navSubVws!
        {
            if (String(describing: subVws).range(of:"UINavigationBarContentView") != nil)
            {
                let barContentSubVws = subVws.subviews
    
                for subVws in barContentSubVws
                {
                    if (String(describing: subVws).range(of:"UISearchBar") != nil)
                    {
                        let searchbarVws = subVws.subviews
    
                        for subVws in searchbarVws
                        {
                            if (String(describing: subVws).range(of:"UIView") != nil)
                            {
                                let searchbarTopSubVws = subVws.subviews
    
                                for subVws in searchbarTopSubVws
                                {
                                    if (String(describing: subVws).range(of:"UISearchBarTextField") != nil)
                                    {
                                        let searchBarVw = subVws as! UITextField
                                        let searchbarTxtFldSubVws = subVws.subviews
    
                                        (subVws as! UITextField).backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0)
                                        (subVws as! UITextField).tintColor = UIColor.white
                                        (subVws as! UITextField).textColor = UIColor.white
                                        (subVws as! UITextField).clearButtonMode = .never
    
    
                                        for subVws in searchbarTxtFldSubVws
                                        {
                                            if (String(describing: subVws).range(of:"UISearchBarTextFieldLabel") != nil)
                                            {
                                                (subVws as! UILabel).textColor = UIColor.white
                                            }
    
                                            if (String(describing: subVws).range(of:"UIImageView") != nil)
                                            {
                                                var searchIconImgVw = (subVws as! UIImageView)
    
                                                searchIconImgVw.image = searchIconImgVw.image!.withRenderingMode(.alwaysTemplate)
                                                searchIconImgVw.tintColor = UIColor.white
    
                                                searchBarVw.leftViewMode = .never
                                                searchBarVw.rightView = searchIconImgVw
                                                searchBarVw.rightViewMode = .always
    
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
    
    
            }
        }
    
    }
    

    Storyboard

    enter image description here

    Output 1

    enter image description here

    Output 2

    enter image description here