ios – 更改UISearchBar搜索字段背景

我在UIView中包含一个UISearchBar,并且UIView已被分配给一个UITableView的tableHeaderView.我试图使UISearchBar的样式看起来像这样:

门外,这是什么样的:

有关如何设计UISearchBar,其搜索字段等的一些SO的问题和答案,有些特别注意背景.其中许多涉及遍历UISearchBar的子视图,找到一个UISearchBarBackground(或_UISearchBarSearchFieldBackgroundView)对象,并直接设置其背景颜色.我试图找到一个支持的API来做到这一点,但是…

setSearchFieldBackgroundImage:forState:方法似乎有很大的潜力,但是当我使用UIControlStateNormal使用1px的白色(或透明)图像时,这是我得到的:

关于如何让这个工作的任何想法?我会感谢有人指着我一个SO的帖子来回答这个问题,但我确实认为我已经阅读了.万分感谢.

解决方法

您可以使用外观WhenContainedIn来定位元素,而不会遍历视图层次结构.就像是:
[[UITextField appearanceWhenContainedIn:[UISearchBar class],nil] setBackgroundColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class],nil] setTextColor:[UIColor lightGrayColor]];

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...