gpt4 book ai didi

ios - 自定义 navigationItem 的 searchController,但不适用于 iOS 11

转载 作者:行者123 更新时间:2023-11-29 11:42:01 24 4
gpt4 key购买 nike

UISearchController iOS 11 customization这个问题我已经搜索过了,但是评论里的方法都帮不了我,所以我想再问一遍。

我使用以下代码设置搜索栏的外观。

扩展 UISearchBr 以获取 textField 和 placeHolderLabel:

extension UISearchBar{

var textField: UITextField?{
if let textField = self.value(forKey: "searchField") as? UITextField {

return textField
}
return nil
}

var placehloderLabel:UILabel?{

if let placeholderLabel = textField?.value(forKey: "placeholderLabel") as? UILabel{
return placeholderLabel
}
return nil
}
}

自定义 UISearchController 的子类:

class CustomSearchController:UISearchController{

override init(searchResultsController: UIViewController?) {

super.init(searchResultsController: searchResultsController)
self.definesPresentationContext = true
self.dimsBackgroundDuringPresentation = false
self.hidesNavigationBarDuringPresentation = true
self.searchBar.searchBarStyle = .minimal

self.searchBar.placeholder = "搜索歌单内歌曲"
self.searchBar.textField?.textColor = UIColor.white
self.searchBar.placehloderLabel?.textColor = .white
self.searchBar.placehloderLabel?.font = UIFont.systemFont(ofSize: 15)

}

设置 prefersLargeTitles UINavigationBar.appearance().prefersLargeTitles = true

如果navigationItem.searchController = searchController结果如下(searchBar的外观不会发生变化): enter image description here

但是如果我设置 navigationItem.titleView = searchController.searchBar,它会: enter image description here

Does the iOS 11 permit developers to change the searchBar's appearance?If yes,I wonder how to customize it ?Any point is appreciated.Thanks!

最佳答案

Working with color and UINavigationItem's search controller

这段代码在 AppDelegate 类中。

UISearchBar.appearance().tintColor = UIColor(named: "Button") // using this to set the text color of the 'Cancel' button since the search bar ignores the global tint color property for some reason

if #available(iOS 11.0, *) {

// Search bar placeholder text color
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "Search", attributes: [NSForegroundColorAttributeName: UIColor.white])

// Search bar text color
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.red]

// Insertion cursor color
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = UIColor.red

} else {
// Fallback on earlier versions
}

// Search bar clear icon
UISearchBar.appearance().setImage(UIImage(named: "clear"), for: .clear, state: .normal)

关于ios - 自定义 navigationItem 的 searchController,但不适用于 iOS 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45991627/

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