how to change UISearchBar from round to rectangle?

后端 未结 12 1553
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 01:54

i will like to know how do i need to change a UISearchBar from the default round curve to a rectangle.

\"enter

12条回答
  •  甜味超标
    2021-02-09 02:35

    The way that it worked with me on Swift 4 is to Subclass the UISearchBar and make the necessary adjustment there without any kind of hacking or workaround ... I have used the Subclass used in this tutorial and it worked like charm

    Here is the code from the example used

    //
    //  SearchBar.swift
    //  Yomu
    //
    //  Created by Sendy Halim on 9/3/17.
    //  Copyright © 2017 Sendy Halim. All rights reserved.
    //
    
    import Foundation
    import UIKit
    
    class SearchBar: UISearchBar {
      override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
    
        searchBarStyle = .minimal
    
        // Create search icon
        let searchIcon = UIImageView(image: #imageLiteral(resourceName: "search"))
        let searchImageSize = searchIcon.image!.size
        searchIcon.frame = CGRect(x: 0, y: 0, width: searchImageSize.width + 10, height: searchImageSize.height)
        searchIcon.contentMode = UIViewContentMode.center
    
        // Configure text field
        let textField = value(forKey: "_searchField") as! UITextField
        textField.leftView = searchIcon
        textField.borderStyle = .none
        textField.backgroundColor = UIColor(hex: "#F7F7F7")
        textField.clipsToBounds = true
        textField.layer.cornerRadius = 6.0
        textField.layer.borderWidth = 1.0
        textField.layer.borderColor = textField.backgroundColor!.cgColor
        textField.textColor = UIColor(hex: "#555555")
      }
    }
    

提交回复
热议问题