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

标签 ios customization uisearchbar uisearchcontroller ios11

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/

相关文章:

Jquery Google map 类插件 - 使用自定义 map /图像和添加标记的能力

json - Openresty自定义json访问日志

ios - 自定义 UISearchController 动画

ios - 选择文本字段时 UISearchBar 会更改背景

ios - 如何将对 tableView 的约束设置为 viewController?

ios - ZIPFoundation:通过数据提供程序将大 PNG 写入存档时出现问题

ios - 如何将运动追踪器 hrm(心率监测器)与 iOS 应用程序连接

css - 在acumatica中自定义css文件以更改表单颜色

iphone - 如何与 HTTP_PROXY 服务器建立 NSStream 套接字连接(服务器也需要身份验证)?

ios - UINavigationController 中的 UISearchBar