Can the height of the UISearchbar TextField be modified?

后端 未结 8 863
-上瘾入骨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 05:07

    This is the correct Swift 4.0 answer on how to change the height of a UISearchBar:

      let image = getImageWithColor(color: UIColor.clear, size: CGSize(width: 1, height: 40))
      searchBar.setSearchFieldBackgroundImage(image, for: .normal)
    

    Using following method:

      func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
            let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
            UIGraphicsBeginImageContextWithOptions(size, false, 0)
            color.setFill()
            UIRectFill(rect)
            let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
            return image
        }
    

    Tested on 4th. June. 2018

    0 讨论(0)
  • Update for Swift 4.2/iOS 12.1:

    let newHeight : CGFloat = 45
    for subview in searchBar.subviews {
        for subsubview in subview.subviews {
            if let textField = subsubview as? UITextField {
                var currentTextfieldBounds = textField.bounds
                currentTextfieldBounds.size.height = newHeight
                textField.bounds = currentTextfieldBounds
                textField.borderStyle = .roundedRect
                textField.contentVerticalAlignment = .center
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题