UISearchBar custom corners

前端 未结 5 1591
星月不相逢
星月不相逢 2020-12-31 06:46

I\'m trying to create a search bar like this:

\"enter

But I\'m noticing that I

相关标签:
5条回答
  • 2020-12-31 07:19

    I am using this code UISearchBar but you can use this code with UISearchController.

        let searchBar = UISearchBar()
                    searchBar.sizeToFit()
                    searchBar.placeholder = "Search"
                    navigationItem.titleView = searchBar
    
                    if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
                        textfield.textColor = UIColor.blue
                        if let backgroundview = textfield.subviews.first {
                            // Background color
                            backgroundview.backgroundColor = UIColor.white
                            // Rounded corner
                            backgroundview.layer.cornerRadius = 14;
                            backgroundview.clipsToBounds = true;
                        }
                    }
    
    0 讨论(0)
  • 2020-12-31 07:19

    You should change the radius of searchTextField inside UISearchBar .

    you can do that like this :

    searchBar.searchTextField.layer.cornerRadius = 20
    searchBar.searchTextField.layer.masksToBounds = true
    

    * searchBar is an outlet of UISearchBar from storyBoard

    0 讨论(0)
  • 2020-12-31 07:27

    ez way for searchbarview

    for subview & POPUPs [Swift 5]

    override func layoutSublayers(of layer: CALayer) {
        searchBarPopup.clipsToBounds = true
        searchBarPopup.layer.cornerRadius = 10
        searchBarPopup.layer.maskedCorners = [ .layerMaxXMinYCorner, .layerMinXMinYCorner]
    }
    

    0 讨论(0)
  • 2020-12-31 07:34

    The issue here is you are setting the corner radius on the UISearchBar, not the UITextField inside it. You can do some sort of hack to get the UITextField, but that's not really recommended.

    As you mentioned in your question, you'll need to use custom images and the methods shown here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/doc/uid/TP40007529-CH3-SW40

    0 讨论(0)
  • 2020-12-31 07:35

    This IS working for me in swift 3 iOS 10:

        searchController.searchBar.layer.cornerRadius = 20
        searchController.searchBar.clipsToBounds = true
    

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