Can the height of the UISearchbar TextField be modified?

后端 未结 8 870
-上瘾入骨i
-上瘾入骨i 2020-12-05 04:34

I\'m testing out my UI and I find the search bar a little bit too narrow for my liking. I also want to make sure people with poorer vision or poorer manual dexterity have n

8条回答
  •  再見小時候
    2020-12-05 04:58

    For swift 5.0 it works good:

    extension UIImage {
        class func image(color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
            return UIGraphicsImageRenderer(size: size).image { rendererContext in
                color.setFill()
                rendererContext.fill(CGRect(origin: .zero, size: size))
            }
        }
    }
    
    extension UISearchBar {
        func customize() {
            setSearchFieldBackgroundImage(UIImage.image(color: .clear, size: CGSize(width: 1, height: 44)), for: .normal)
    
            // other settings
            searchBarStyle = .minimal
            searchTextPositionAdjustment = UIOffset(horizontal: 0, vertical: 0)
            tintColor = .black
            backgroundColor = .whiteBackground
            searchTextField.leftViewMode = .never
            searchTextField.textColor = .black
            searchTextField.backgroundColor = UIColor.lightGrayBackground
            searchTextField.layer.borderColor = UIColor.gray.cgColor
            searchTextField.cornerRadius = 22
            searchTextField.borderWidth = 0.5
        }
    }
    

    Just use:

    let searchBar = UISearchBar()
    searchBar.customize()
    

提交回复
热议问题