iOS 11中的UINavigationBar中的UISearchBar [英] iOS 11 UISearchBar in UINavigationBar

查看:257
本文介绍了iOS 11中的UINavigationBar中的UISearchBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to place a search bar in the new navigation bar with the new iOS 11 large titles. However, the color of the search bar is automatically applied by iOS and I can't change it.

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

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

The search bar get's a deep blue background, but I just want to change it to white.

设置背景色会导致以下结果:

Setting the background color results in this:

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

我也在搜索栏上尝试过setScopeBarButtonBackgroundImagesetBackgroundImage,但是一切看起来都很奇怪.

I have tried also setScopeBarButtonBackgroundImage and setBackgroundImage on the search bar but everything looks totally weird.

此外,当我通过点击搜索栏触发搜索时,它会切换到右侧带有取消"按钮的模式. (德语为"Abbrechen")

Also when I trigger the search by tapping on the search bar, it switches to a mode with the cancel button on the right side. ("Abbrechen" in German)

"Abbrechen"文字颜色也无法更改. (还需要白色)

And the "Abbrechen" text color also can't be changed. (need it also white)

感谢您的帮助.

根据要求,在此处提供导航栏样式的代码:

As requested, here the code for the navigation bar styling:

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的想法设置搜索栏背景的颜色,但是圆角丢失了.重新设置圆角后,搜索栏的动画似乎已损坏.

Current outcome: I have used Krunals idea to set the color of the search bar background but then the rounded corners are lost. After re-setting the rounded corners the animation of the search bar seems to be broken.

因此仍然没有令人满意的解决方案.似乎无法自定义嵌入在iOS 11中导航栏中的搜索栏.同时,仅更改占位符文本的颜色就足够了,但即使这样做似乎也不可行. (我已经尝试过StackOverflow的多种方法-不起作用)

So still no satisfactory solution. Seems that the search bar when embedded into the navigation bar in iOS 11 is not possible to customize. Meanwhile it would be enough for me to just change the color of the placeholder text, but even this seems not to be possible. (I have tried multiple approaches from StackOverflow - doesn't work)

推荐答案

现在这就是您想要的...

Now it's what you want...

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

}

结果:


带有圆角:
带有圆角的动画也可以正常工作.


With Rounded corner:
Animation with rounded corner is also working fine.

这篇关于iOS 11中的UINavigationBar中的UISearchBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆