簡體   English   中英

不能改變 UISearchBar 的顏色?

[英]Can not change color of UISearchBar?

我正在嘗試更改我的 searchBar 的 background-、tint- 和 barTintColor...我嘗試了很多不同的方法,但都沒有奏效。 我什至嘗試將 GlobalTintColor 設置為我想要的黃色。

當前代碼:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    //UISearchController
    searchController.searchBar.delegate = self
    self.searchController.delegate = self
    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.sizeToFit()
    searchController.searchBar.barTintColor = UIColor(red:1.00, green:0.96, blue:0.02, alpha:1.0)
    searchController.searchBar.tintColor = UIColor(red:1.00, green:0.96, blue:0.02, alpha:1.0)
    searchController.searchBar.backgroundColor = UIColor(red:0.36, green:0.39, blue:0.40, alpha:1.0)
    searchController.searchBar.placeholder = "Search for Users"

    if #available(iOS 9.1, *) {
        searchController.obscuresBackgroundDuringPresentation = false
    } else {
        // Fallback on earlier versions
    }

    if #available(iOS 11.0, *) {
        navigationItem.searchController = searchController
    } else {
        // Fallback on earlier versions
    }

    if #available(iOS 11.0, *) {
        self.navigationItem.searchController = self.searchController
        self.navigationItem.hidesSearchBarWhenScrolling = false
    } else {
        tableView.tableHeaderView = searchController.searchBar
    }
    self.navigationItem.titleView?.tintColor = UIColor.yellow
}

我有什么設置錯誤嗎?

這樣做:

func SearchBarAppearence() {
    searchController.searchBar.tintColor = .black
    searchController.searchBar.setValue("text of button", forKey:"_cancelButtonText")
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "text of searchbar", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont(name: IRAN_SANS_MOBILE_FONT, size: 14)!]
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: IRAN_SANS_MOBILE_FONT, size: 14)!], for: .normal)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM