gpt4 book ai didi

ios - 如何将自定义搜索栏放在导航栏上?

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

我想将自定义搜索栏放在导航栏上,而不是放在导航栏下。

我使用SHSearchBar .

https://github.com/Blackjacx/SHSearchBar

myStoryboard and Simulator's Image is like this

var searchBar4: SHSearchBar!

var rasterSize: CGFloat = 11.0
let searchbarHeight: CGFloat = 44.0

override func viewDidLoad() {
super.viewDidLoad()

let searchGlassIconTemplate = UIImage(named: "icon-search")!.withRenderingMode(.alwaysTemplate)

// TODO: SearchBar4: centered text lets the icon on the left - this is not intended!
let leftView4 = imageViewWithIcon(searchGlassIconTemplate, rasterSize: rasterSize)
searchBar4 = defaultSearchBar(withRasterSize: rasterSize, leftView: leftView4, rightView: nil, delegate: self)
searchBar4.textAlignment = .center
searchBar4.text = NSLocalizedString("sbe.exampleText.centered", comment: "")

view.addSubview(searchBar4)

searchBar4.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: rasterSize).isActive = true
searchBar4.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: rasterSize).isActive = true
searchBar4.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -rasterSize).isActive = true
searchBar4.heightAnchor.constraint(equalToConstant: searchbarHeight).isActive = true
}

func imageViewWithIcon(_ icon: UIImage, rasterSize: CGFloat) -> UIImageView {
let imgView = UIImageView(image: icon)
imgView.frame = CGRect(x: 0, y: 0, width: icon.size.width + rasterSize * 2.0, height: icon.size.height)
imgView.contentMode = .center
imgView.tintColor = UIColor(red: 235/255, green: 235/255, blue: 241/255, alpha: 1)
return imgView
}

func defaultSearchBar(withRasterSize rasterSize: CGFloat, leftView: UIView?, rightView: UIView?, delegate: SHSearchBarDelegate, useCancelButton: Bool = true) -> SHSearchBar {
var config = defaultSearchBarConfig(rasterSize)
config.leftView = leftView
config.rightView = rightView
config.useCancelButton = useCancelButton

if leftView != nil {
config.leftViewMode = .always
}

if rightView != nil {
config.rightViewMode = .unlessEditing
}

let bar = SHSearchBar(config: config)
bar.delegate = delegate
bar.placeholder = NSLocalizedString("sbe.textfieldPlaceholder.default", comment: "")
bar.updateBackgroundImage(withRadius: 6, corners: [.allCorners], color: UIColor.white)
bar.layer.shadowColor = UIColor.black.cgColor
bar.layer.shadowOffset = CGSize(width: 0, height: 3)
bar.layer.shadowRadius = 5
bar.layer.shadowOpacity = 0.25
return bar
}

func defaultSearchBarConfig(_ rasterSize: CGFloat) -> SHSearchBarConfig {
var config: SHSearchBarConfig = SHSearchBarConfig()
config.rasterSize = rasterSize
// config.cancelButtonTitle = NSLocalizedString("sbe.general.cancel", comment: "")
config.cancelButtonTextAttributes = [.foregroundColor : UIColor.darkGray]
config.textContentType = UITextContentType.fullStreetAddress.rawValue
config.textAttributes = [.foregroundColor : UIColor.gray]
return config
}

如何将 SHSearchBar 放在导航栏上?

最佳答案

您可以将搜索栏放在导航的 titleView 中。

 lazy var searchBar = UISearchBar(frame: .zero)
searchBar.placeholder = "Your PlaceHolder"
navigationItem.titleView = searchBar

希望它可以帮助..:)

关于ios - 如何将自定义搜索栏放在导航栏上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51667535/

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