gpt4 book ai didi

ios - 更改 UISearchBar 放大图标颜色和位置

转载 作者:可可西里 更新时间:2023-11-01 00:23:00 27 4
gpt4 key购买 nike

我有一个 UISearchBar,我想更改初始放大图标(出现在 UISearchBar 中间的那个)的位置以及颜色或图标。

到目前为止,我更改了色调和图标图像。

但是,只有当我在模拟器上测试应用程序时才会显示新图标,但在实际设备(均运行 iOS 9.3)上它仍然显示默认图标。

UISearchBar.appearance().setImage(UIImage(named: "SearchbarIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)

至于放大图标的位置,我希望它在左侧,如果我激活 UISearchBar,它就会出现。

enter image description here

enter image description here

我在这里找到了很多答案,其中许多提供的解决方案在 iOS 8.0+ 上不起作用或在 Objective-C 中,我在理解它们时遇到了一些问题。

我尝试添加一个包含图标的自定义背景,但它显示在搜索栏的底部,而图标仍然存在:

enter image description here

如果我将 Y 的背景偏移量更改为 -44(UISearchBar 的高度),背景显示正常,但我无法再键入,因为似乎整个文本字段都被推高了。我还尝试将 SearchText 的垂直偏移更改为 44 以进行补偿,但没有成功。老实说,我不确定搜索文本和背景偏移量应该做什么,但我决定试一试。

enter image description here

有什么办法可以实现吗?或者可能采用不同的方法?

最佳答案

您可以使用

调整搜索栏图标的位置
func positionAdjustmentForSearchBarIcon(_ icon: UISearchBarIcon) -> UIOffset

参见 https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/occ/instm/UISearchBar/positionAdjustmentForSearchBarIcon :

关于ios - 更改 UISearchBar 放大图标颜色和位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36576041/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com