当搜索栏处于活动状态和禁用状态时,如何更改搜索栏的颜色和表视图的颜色?

时间:2018-08-24 15:13:47

标签: ios swift xcode

See screenshot here

如何更改背景颜色(而不是搜索栏文本字段)

我还如何增加状态栏和搜索栏之间的距离?看起来太近了。

我已经尝试过了,但是没有按预期工作:

 func configureSearchController ()
    {
        searchController = UISearchController(searchResultsController: resultsController)
        searchController.searchResultsUpdater = self

        searchController.searchBar.layer.borderWidth = 0;

        //searchController.searchBar.searchBarStyle = .minimal

        searchController.searchBar.barTintColor = UIColor.searchBarBackgroundGrey()



        for subView in searchController.searchBar.subviews {
            for subViewOne in subView.subviews {
                if subViewOne is UITextField {
                    subViewOne.backgroundColor = UIColor.searchBarTextFieldGrey()
                    break
                }
            }
        }

    }

    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        searchBar.barTintColor = UIColor.white

    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
      searchBar.barTintColor = UIColor.searchBarBackgroundGrey()

    }

1 个答案:

答案 0 :(得分:0)

您需要在viewDidLoad()方法中设置搜索栏的委托:

searchController.searchBar.delegate = self