[英]iOS 11 UINavigationBar UISearchBar and UIBarButtonItem overlap
[英]iOS 11 UISearchBar in UINavigationBar
我想在帶有新 iOS 11 大標題的新導航欄中放置一個搜索欄。 但是,搜索欄的顏色是由 iOS 自動應用的,我無法更改它。
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
搜索欄是深藍色背景,但我只想將其更改為白色。
設置背景顏色會導致:
navigationItem.searchController?.searchBar.backgroundColor = UIColor.white
我也在搜索欄上嘗試過setScopeBarButtonBackgroundImage
和setBackgroundImage
,但一切看起來都很奇怪。
此外,當我通過點擊搜索欄觸發搜索時,它會切換到右側帶有取消按鈕的模式。 (德語中的“Abbrechen”)
並且“Abbrechen”文本顏色也無法更改。 (也需要白色)
任何幫助表示贊賞。
編輯:根據要求,這里是導航欄樣式的代碼:
self.navigationBar.tintColor = UIColor.myWhite
self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.myWhite, NSAttributedStringKey.font: UIFont.myNavigationBarTitle()]
self.navigationBar.barTintColor = UIColor.myTint
if #available(iOS 11.0, *) {
self.navigationBar.prefersLargeTitles = true
self.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.myWhite, NSAttributedStringKey.font: UIFont.myNavigationBarLargeTitle()]
}
當前結果:我使用 Krunals 的想法來設置搜索欄背景的顏色,但是圓角丟失了。 重新設置圓角后,搜索欄的動畫似乎被破壞了。
所以仍然沒有令人滿意的解決方案。 似乎無法自定義嵌入到 iOS 11 導航欄中的搜索欄。 同時,我只需更改占位符文本的顏色就足夠了,但即使這樣似乎也不可能。 (我已經嘗試了 StackOverflow 的多種方法 - 不起作用)
現在這就是你想要的......
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
let scb = sc.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.blue
}
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
結果:
帶圓角:
帶圓角的動畫也工作正常。
我在 AppDelegate 中使用此代碼更改了文本字段的背景。
斯威夫特 4
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//background color of text field
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan
}
這是結果
這應該適合你
func addSearchbar(){
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
let scb = sc.searchBar
scb.tintColor = UIColor.white
if let navigationbar = self.navigationController?.navigationBar {
//navigationbar.tintColor = UIColor.green
//navigationbar.backgroundColor = UIColor.yellow
navigationbar.barTintColor = UIColor.blue
}
navigationController?.navigationBar.tintColor = UIColor.green
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
}
結果:
目標 C
if (@available(iOS 11.0, *)) {
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.navigationItem.searchController=self.searchController;
self.navigationItem.hidesSearchBarWhenScrolling=NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;
self.searchController.searchBar.showsBookmarkButton = NO;
self.searchController.searchBar.placeholder = @"Search";
self.searchController.searchBar.tintColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
self.searchController.searchBar.barTintColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:1];
UITextField *txfSearchField = [self.searchController.searchBar valueForKey:@"_searchField"];
txfSearchField.tintColor=[UIColor colorWithRed:21/255.0 green:157/255.0 blue:130/255.0 alpha:1];
txfSearchField.textColor=[UIColor colorWithRed:1 green:1 blue:1 alpha:1];
txfSearchField.backgroundColor=[UIColor whiteColor];
UIView *backgroundview= [[txfSearchField subviews]firstObject ];
backgroundview.backgroundColor=[UIColor whiteColor];
// Rounded corner
backgroundview.layer.cornerRadius = 8;
backgroundview.clipsToBounds = true;
}
這是我用來使搜索欄變白的代碼:
if let textfield = searchController.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundview = textfield.subviews.first {
backgroundview.backgroundColor = UIColor.init(white: 1, alpha: 1)
backgroundview.layer.cornerRadius = 10
backgroundview.clipsToBounds = true
}
}
在 ios 13 上測試
搜索欄上的完美白色背景
func setupSearchbarController(){
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
let scb = sc.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.white
textfield.backgroundColor = UIColor.white
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = AppColor.themeColor
}
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
}
試試這個代碼,
UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.