ios - 如何将UISearchbar做成矩形并与导航栏合并?

标签 ios swift uinavigationcontroller uinavigationbar uisearchbar

您好,我正在尝试将 UISearchbar 更改为矩形而不是圆角矩形,并将其与具有相同颜色的导航栏合并。

enter image description here

导航栏的背景颜色应与搜索栏的背景颜色相同,但有区别,并且显示清晰的边缘,两者都应显示背景颜色 #be1b25 而不是 #bc303a。文本字段仍然显示圆角矩形而不是完整的矩形。

主视图 Controller 代码

   @IBOutlet weak var searchBar: UISearchBar!
    self.navigationController?.navigationBar.isTranslucent = true
    navigationController?.navigationBar.backgroundColor = UIColor().HexToColor(hexString: REDCOLOR)
    searchBar.backgroundColor = UIColor().HexToColor(hexString: REDCOLOR)
    searchBar.tintColor = UIColor().HexToColor(hexString: REDCOLOR)
    searchBar.barTintColor = UIColor().HexToColor(hexString: REDCOLOR)
    searchBar.barStyle = .default
    searchBar.placeholder = NetworkManager.sharedInstance.language(key: "searchentirestore")
    searchBar.layer.cornerRadius = 0;
    searchBar.clipsToBounds = false;
    searchBar.barTintColor = UIColor().HexToColor(hexString: REDCOLOR)
    searchBar.layer.borderWidth = 0
    //searchBar.layer.borderColor = UIColor.clear as! CGColor

UISearchBar 扩展

extension UISearchBar {

    private var textField: UITextField? {
        return subviews.first?.subviews.flatMap { $0 as? UITextField }.first
    }

    private var activityIndicator: UIActivityIndicatorView? {
        return textField?.leftView?.subviews.flatMap{ $0 as? UIActivityIndicatorView }.first
    }

    var isLoading: Bool {
        get {
            return activityIndicator != nil
        } set {
            if newValue {
                if activityIndicator == nil {
                    let newActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
                    newActivityIndicator.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
                    newActivityIndicator.startAnimating()
                    newActivityIndicator.color = UIColor().HexToColor(hexString: BUTTON_COLOR)
                    newActivityIndicator.backgroundColor = UIColor.white
                    textField?.leftView?.addSubview(newActivityIndicator)
                    let leftViewSize = textField?.leftView?.frame.size ?? CGSize.zero
                    newActivityIndicator.center = CGPoint(x: leftViewSize.width/2, y: leftViewSize.height/2)
                }
            } else {
                activityIndicator?.removeFromSuperview()
            }
        }
    }

请帮忙,这是 Swift 3。提前致谢

最佳答案

所以您正在处理一些事情

1 矩形UISearchbar

  • 您将遍历 native UISearchbar 的 subview 并更改其右子层角半径,但这是一个黑客,Apple 可以更改该层次结构,您将处于重新开始
  • 您可以创建自己的组件,其外观类似于 UISearchbar、一些 UIView 以及带有一些 UIImageView 的 UITextfield
  • GitHub 上几乎没有可供使用的库 https://github.com/CosmicMind/Samples
  • 这里也有类似的问题iOS 11 UISearchBar in UINavigationBar

2 与导航栏合并

  • 一种方法是在 iOS 11 中使用 UISearchController
  • 或者您可以使用自定义 UISearchbar,将其添加到 UIView 中的导航栏下方,而不是导航中,但在这种情况下,导航栏不能是半透明的,因为您永远不会获得相同的色彩空间
  • 您可以将自定义 View 作为自定义 UIView 添加到导航栏中的 titleView

关于ios - 如何将UISearchbar做成矩形并与导航栏合并?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51683215/

相关文章:

ios - 按钮操作不适用于 subview

ios - 自定义Tableview滑动 Action

ios - 使用 swift 放大到 PDFKit 中的特定坐标

ios - 在像 Twitter 这样的每个 View Controller 中使用不同颜色的导航栏(不使用 setbartintcolor)

ios - 使用模拟数据测试 URLSession 委托(delegate)

ios - Realm Relation如何实现

ios - 在 Xamarin iOS 上获取系统语言设置

swift - 更改 UIButton alpha 然后无法识别点击

swift - ViewDidLoad() 中的导航栏高度未以编程方式更改?

ios - 使用导航 Controller 在滑出面板中显示右键