How to add a 1 pixel gray border around a UISearchBar TextField

后端 未结 6 2412
攒了一身酷
攒了一身酷 2021-02-19 14:21

I\'m trying to figure out how to add a 1 pixel stroke gray border to my UISearchBar in my app. The Facebook Messenger app accomplishes this quite well. (see pic below).

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-19 14:37

    Swift 5

    Easy way to do

        @IBOutlet private weak var searchBar:UISearchBar!{
            didSet{
                if let searchTextfield = self.searchBar.value(forKey: "searchField") as? UITextField  {
                    searchTextfield.layer.borderColor = UIColor.lightGray.cgColor
                    searchTextfield.layer.borderWidth = 1
                    searchTextfield.layer.cornerRadius = 10
                }
            }
        }
    
    

提交回复
热议问题