Atlantis
Atlantis

Reputation: 775

swift iOS13 searchBar textfield height change

any chances to change UISearchBar's UITextField height? I tried almost all solutions which I've could found, but all are unsuccessful.

Updated: I realized that ios13 searchBar doesn't contains UITextField, but contains UISearchBarTextField class

enter image description here

and here is hierarchy

enter image description here

as I understood we have to looking for the new class for iOS13 instead of UITextField

I've tried looping and search TextField

    override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    for subView in searchBar.subviews {
        for subsubView in subView.subviews{
            for subsubsubView in subsubView.subviews{
                if #available(iOS 13.0, *) {
                    if let textField = subsubsubView as? UISearchTextField{
                        var bounds: CGRect
                        bounds = textField.frame
                        bounds.size.height = 40 //(set height whatever you want)
                        textField.bounds = bounds
                        textField.layoutIfNeeded()
                    }
                } else {
                    if let textField = subsubView as? UITextField {
                        var bounds: CGRect
                        bounds = textField.frame
                        bounds.size.height = 40 //(set height whatever you want)
                        textField.bounds = bounds

                    }
                }
            }
        }
    }
}

and debugger goes into condition and found UITextField and changed height, but on device - there no height changes

Upvotes: 3

Views: 1144

Answers (1)

miya
miya

Reputation: 1069

What I finally did was to set an UIImageView as a container for the UISearchBar.

Didn't work with a simple UIView, I guess it had to do with intrinsic content size?

    func showSearchBar() {

        self.searchNavigationItem?.setRightBarButton(nil, animated: false)


        guard let searchBar = self.searchBar else {

            assert(false, "Need a search bar to display")
            return
        }


        self.searchContainer = UIImageView()
        self.searchContainer?.isUserInteractionEnabled = true

        let image = // Some image with the corresponding size.


        self.searchContainer?.image = image
        self.searchContainer?.alpha = 0

        self.searchContainer?.frame = // Same size as the image

        // Here you add the searchContainer into the titleView.
        self.searchNavigationItem?.titleView = self.searchContainer

        searchBar.translatesAutoresizingMaskIntoConstraints = false

        // Create top/bottom/leading/trailing constraints

        self.searchContainer?.addSubview(searchBar)
        searchBar.sizeToFit()

        self.searchContainer?.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])

         self?.searchContainer?.alpha = 1

    }

Upvotes: 1

Related Questions