不能更改searchBar的颜色,选择时会跳到视图顶部

问题描述 投票:0回答:1

我试图将搜索栏附加到tableView的顶部并更改其属性(例如颜色,占位符)。但是,我不知道如何。我尝试将tableView嵌入另一个视图中,但这没有帮助。有什么想法吗?

func setupSearch(){
        search.delegate = self
        search.automaticallyShowsCancelButton = false
        search.searchBar.tintColor = UIColor.red
        search.searchBar.barTintColor = UIColor.red
        search.obscuresBackgroundDuringPresentation = false
        search.hidesNavigationBarDuringPresentation = false
        search.searchBar.placeholder = "Type something here to search"
        navigationItem.searchController = search
        tableView.tableHeaderView = search.searchBar
    }

此函数在viewDidLoad()中调用,并且添加了tableView,但没有正确的颜色或占位符,并且在选定时跳转到屏幕顶部。任何帮助,将不胜感激。

swift uisearchbar ios11
1个回答
0
投票
override func viewDidLoad() {
    super.viewDidLoad()

  setupSearch()
 NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

   }


@objc func keyboardWillShow(notification: NSNotification) {
   tableView.frame.origin.y = 0

}


 func setupSearch(){
 let textFieldSearchBar = searchBar.value(forKey: "searchField") as? UITextField
  textFieldSearchBar?.textColor = .red

  let searchBarLabel = textFieldSearchBar!.value(forKey: "placeholderLabel") as? UILabel
  textFieldSearchBarLabel?.textColor = .red

 }
© www.soinside.com 2019 - 2024. All rights reserved.