Swift-以编程方式自定义UISearchBar设计

Swift-以编程方式自定义UISearchBar设计,swift,Swift,我正在尝试以编程方式自定义UISearchBar设计。我试着添加了一堆代码,但只更改了几个地方 这就是我想要创造的设计 这就是它现在的样子 我从模拟器上截取了第二幅图像,所以它更大 无论如何,我正在努力使我的搜索栏 浅色背景色 高度较短的文本字段 Textfield具有较少的拐角radious 这是我的密码: let searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, hei

我正在尝试以编程方式自定义UISearchBar设计。我试着添加了一堆代码,但只更改了几个地方

这就是我想要创造的设计

这就是它现在的样子

我从模拟器上截取了第二幅图像,所以它更大

无论如何,我正在努力使我的搜索栏

  • 浅色背景色
  • 高度较短的文本字段
  • Textfield具有较少的拐角radious
  • 这是我的密码:

    let searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 60))
    
        [In viewDidLoad()]
        searchBar.delegate = self
        searchBar.isTranslucent = false
        searchBar.placeholder = "Search the phrase"
        searchBar.tintColor = UIColor(red:0.73, green:0.76, blue:0.78, alpha:1.0)
        searchBar.setImage(UIImage(named: "search"), for: .search, state: .normal)
    
        let forTextField = searchBar.value(forKey: "searchField") as? UITextField
        forTextField?.textColor = UIColor(red:0.31, green:0.31, blue:0.31, alpha:1.0)
        forTextField?.font = UIFont(name: "HelveticaNeue-Light", size: 14)
        forTextField?.clipsToBounds = true
        forTextField?.layer.cornerRadius = 5
        forTextField?.frame.size.height = 30
    

    谢谢你的帮助

    以下是实现这一目标的方法:

    class ViewController: UIViewController, UISearchBarDelegate {
    
        let searchBar = UISearchBar(frame: CGRect(x: 0, y: 20, width: UIScreen.main.bounds.width, height: 60))
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            searchBar.delegate = self
            searchBar.isTranslucent = false
            searchBar.placeholder = "Search the phrase"
            searchBar.tintColor = UIColor(red:0.73, green:0.76, blue:0.78, alpha:1.0)
    
            //1. Lighter background color
            searchBar.barTintColor = UIColor(netHex: 0xE6E6E6)
    
            self.view.addSubview(searchBar)
        }
    
        override func viewDidAppear(_ animated: Bool) {
            for subView in searchBar.subviews  {
                for subsubView in subView.subviews  {
                    if let textField = subsubView as? UITextField {
                        var bounds: CGRect
                        bounds = textField.frame
    
                        //2. Shorter textfield in height
                        bounds.size.height = 28
                        textField.bounds = bounds
    
                        //3. Textfield to have less corner radious
                        textField.layer.cornerRadius = 5 //Probably you can play with this and see the changes.
                        textField.clipsToBounds = true
                    }
                }
            }
        }
    }
    
    extension UIColor {
        convenience init(r: Int, g: Int, b: Int, a: Int = 255) {
            self.init(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(a) / 255.0)
        }
    
        convenience init(netHex:Int) {
            self.init(r:(netHex >> 16) & 0xff, g:(netHex >> 8) & 0xff, b:netHex & 0xff)
        }
    }
    

    以下是实现这一目标的方法:

    class ViewController: UIViewController, UISearchBarDelegate {
    
        let searchBar = UISearchBar(frame: CGRect(x: 0, y: 20, width: UIScreen.main.bounds.width, height: 60))
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
    
            searchBar.delegate = self
            searchBar.isTranslucent = false
            searchBar.placeholder = "Search the phrase"
            searchBar.tintColor = UIColor(red:0.73, green:0.76, blue:0.78, alpha:1.0)
    
            //1. Lighter background color
            searchBar.barTintColor = UIColor(netHex: 0xE6E6E6)
    
            self.view.addSubview(searchBar)
        }
    
        override func viewDidAppear(_ animated: Bool) {
            for subView in searchBar.subviews  {
                for subsubView in subView.subviews  {
                    if let textField = subsubView as? UITextField {
                        var bounds: CGRect
                        bounds = textField.frame
    
                        //2. Shorter textfield in height
                        bounds.size.height = 28
                        textField.bounds = bounds
    
                        //3. Textfield to have less corner radious
                        textField.layer.cornerRadius = 5 //Probably you can play with this and see the changes.
                        textField.clipsToBounds = true
                    }
                }
            }
        }
    }
    
    extension UIColor {
        convenience init(r: Int, g: Int, b: Int, a: Int = 255) {
            self.init(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(a) / 255.0)
        }
    
        convenience init(netHex:Int) {
            self.init(r:(netHex >> 16) & 0xff, g:(netHex >> 8) & 0xff, b:netHex & 0xff)
        }
    }
    

    非常感谢。除“textField.layer.cornerRadius”外,此选项有效。当我将其设置为2或3时,角仍然是圆的。是的,我已检查它是否不适用于该最小值。可能是一个bug或限制之类的@谢谢你。我还注意到,当我使用您提供的代码时,我的范围图标的偏移量在第二次访问搜索页面时发生了变化。我认为问题在ViewDidEmerge部分,但我不能指出它。你知道为什么吗?这是我的密码。searchBar.setPositionAdjustment(UIOffset(水平:8,垂直:1),for:.search)嗨,我不知道如何在评论部分添加图片,所以我现在把它们放在你的回复中。非常感谢。你能分享这个演示项目吗?谢谢!除“textField.layer.cornerRadius”外,此选项有效。当我将其设置为2或3时,角仍然是圆的。是的,我已检查它是否不适用于该最小值。可能是一个bug或限制之类的@谢谢你。另外,当我使用您提供的代码时,范围图标的偏移量会从搜索页面的第二次访问中更改。我认为问题在ViewDidEmerge部分,但我不能指出它。你知道为什么吗?这是我的密码。searchBar.setPositionAdjustment(UIOffset(水平:8,垂直:1),for:.search)嗨,我不知道如何在评论部分添加图片,所以我现在把它们放在你的回复中。非常感谢。你能分享这个演示项目吗?