【问题标题】:Customize UISearchBar in Swift 4在 Swift 4 中自定义 UISearchBar
【发布时间】:2018-01-27 20:40:03
【问题描述】:
  • 我需要删除搜索栏的背景。
  • 输入文本的部分(白色)使其更高。
  • 在白色部分周围加上绿色边框。
  • 自定义字体。

我需要这个:

我所取得的成就是这样的:

我的代码:

@IBOutlet weak var searchBar: UISearchBar!

override func viewDidLoad() {
    super.viewDidLoad()

    self.searchBar.layer.borderColor = #colorLiteral(red: 0.2352941176, green: 0.7254901961, blue: 0.3921568627, alpha: 1)
    self.searchBar.layer.borderWidth = 1

    self.searchBar.clipsToBounds = true
    self.searchBar.layer.cornerRadius = 20
}

【问题讨论】:

  • 我认为文本字段比搜索栏更适合您的情况。搜索栏具有特定的外观和感觉,您无法轻易更改它。
  • 我已经管理过了,但我无法操作文本的大小或字体。
  • 无论您与解决方案有多接近,选择文本字段都会使您的代码更好读,并且将来更容易修改。我知道您决心使用搜索栏,但我认为它不是最适合您的用例。

标签: ios iphone swift customization swift4


【解决方案1】:

尝试使用此代码:

class JSSearchView : UIView {

    var searchBar : UISearchBar!

    override func awakeFromNib()
    {
        // the actual search barw
        self.searchBar = UISearchBar(frame: self.frame)

        self.searchBar.clipsToBounds = true

        // the smaller the number in relation to the view, the more subtle
        // the rounding -- https://www.hackingwithswift.com/example-code/calayer/how-to-round-the-corners-of-a-uiview
        self.searchBar.layer.cornerRadius = 5

        self.addSubview(self.searchBar)

        self.searchBar.translatesAutoresizingMaskIntoConstraints = false

        let leadingConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 20)
        let trailingConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: -20)
        let yConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)

        self.addConstraints([yConstraint, leadingConstraint, trailingConstraint])

        self.searchBar.backgroundColor = UIColor.clear
        self.searchBar.setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
        self.searchBar.tintColor = UIColor.clear
        self.searchBar.isTranslucent = true

        // https://stackoverflow.com/questions/21191801/how-to-add-a-1-pixel-gray-border-around-a-uisearchbar-textfield/21192270
        for s in self.searchBar.subviews[0].subviews {
            if s is UITextField {
                s.layer.borderWidth = 1.0
                s.layer.cornerRadius = 10
                s.layer.borderColor = UIColor.green.cgColor
            }
        }
    }

    override func draw(_ rect: CGRect) {
        super.draw(rect)

        // the half height green background you wanted...            
        let topRect = CGRect(origin: .zero, size: CGSize(width: self.frame.size.width, height: (self.frame.height / 2)))
        UIColor.green.set()
        UIRectFill(topRect)
    }
}

若要使用它,请将自定义视图拖放到您的 xib 或故事板文件中,并将自定义类类型设置为类的名称 (JSSearchView)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-07
    • 2011-01-29
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多