2017-09-01 129 views
14

我想用新的iOS 11大标题在新的导航栏中放置搜索栏。但是,搜索栏的颜色由iOS自动应用,我无法更改它。iOS 11 UISearchBar在UINavigationBar

if #available(iOS 11.0, *) { 
    let sc = UISearchController(searchResultsController: nil) 
    navigationItem.searchController = sc 
    navigationItem.hidesSearchBarWhenScrolling = false 
} 

搜索栏的深蓝色背景,但我只是想将其更改为白色。

enter image description here

设置背景颜色的结果是:

navigationItem.searchController?.searchBar.backgroundColor = UIColor.white 

enter image description here

我也试图在搜索栏上setScopeBarButtonBackgroundImagesetBackgroundImage,但一切看起来完全怪异。

此外,当我通过点击搜索栏触发搜索时,它会切换到右侧取消按钮的模式。 ( “Abbrechen” 在德语)

enter image description here

与 “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主意设置的搜索栏背景的颜色,但随后圆角丢失。重新设置圆角后,搜索栏的动画似乎被破坏了。

enter image description here

所以还没有满意的解决方案。似乎嵌入到iOS 11导航栏中的搜索栏无法自定义。与此同时,只需更改占位符文本的颜色就足够了,但即使这似乎也不可行。 (我试图从StackOverflow的多种方法 - 不工作)

+0

你需要搜索的文字也是白色或其他 –

+0

搜索文本可以是灰色的,这是确定。但如果这也是可以改变的话,那将很酷。 – Darko

回答

13

现在就是你想要的...

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 

} 

结果:

enter image description here

enter image description here


随着圆角:
动画与圆润的边角也工作正常。

enter image description here

+0

是的,渴望成功,谢谢!圆形的边缘也可以在view.layer我想。完成这一点,我会将它标记为已回答,供其他人找到。有关内部工作的内部知识也不是实现它的最佳方式,但如果这是唯一的方法... – Darko

+0

我试图设置圆角层并且它可以工作。但是当滚动搜索栏时动画看起来很奇怪。总的来说,所有这些看起来像是黑客,并不是很好的工作。 – Darko

+0

与我面临的问题分享我的示例项目。 – Krunal

0

试试这个代码,

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"]; 
txfSearchField.backgroundColor = [UIColor redColor]; 
+0

'if let textField = navigationItem.searchController?.searchBar.value(forKey:“_searchField”)as? UITextField {textField.backgroundColor = UIColor.white }' - 这不起作用。 – Darko

1

这应该

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 
     } 
} 


你结果工作:

enter image description here

enter image description here

+0

这正是我已经拥有的。我需要搜索栏有一个_white_背景。 – Darko

+1

这真的好像在iOS 11中实际上是不可能的。 – Darko

1

我改变了文本字段的背景里面的AppDelegate此代码。

夫特4

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 

     //background color of text field 
     UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan 

     } 

这是结果

example used to change the background to cyan color

2

目标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; 
} 
+0

欢迎来到Stack Overflow!感谢您的代码片段,它可能会提供一些有限的即时帮助。通过展示*为什么*这是一个很好的解决方案,并且使它对未来的读者更有用,一个正确的解释[将大大提高](// meta.stackexchange.com/q/114762)其长期价值其他类似的问题。请[编辑]你的答案以添加一些解释,包括你所做的假设。 –

0

这是我用于使搜索栏白色的代码:

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 
      } 
     } 

enter image description here

相关问题