gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:16:40 24 4
gpt4 key购买 nike

您好,我正在尝试将 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com