更改UISearchBar放大图标颜色和位置 [英] Change UISearchBar magnify icon color and position

查看:638
本文介绍了更改UISearchBar放大图标颜色和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a UISearchBar and I would like to change the position of the initial magnify icon (the one that appears in the middle of the UISearchBar) as well as the color or icon.

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

So far I changed the tint and the icon image.

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

However, the new icon shows up only if I test the app on a simulator but on an actual device(both run iOS 9.3) it still shows the default icon.

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

至于放大图标位置,我想要它在左侧,它显示在哪里如果我激活UISearchBar,我就会起来。

As for the magnify icon position, I want it in the left side, where it shows up if I activate the UISearchBar.

< a href =https://i.stack.imgur.com/LPHFF.png =nofollow noreferrer>

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

I found quite a lot of answers around here many of the provided solutions don't work on iOS 8.0+ or are in Objective-C and I am having some problems understanding them.

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

I tried to add a custom background containing the icon but it shows up at the bottom of the search bar while the icon is still there:

如果背景显示OK我将Y的背景偏移更改为-44(UISearchBar的高度)但我无法再键入,因为看起来整个文本字段都被推高了。我还尝试将SearchText的垂直偏移更改为44以补偿但没有运气。说实话,我不确定搜索文本和背景偏移应该做什么,但我决定尝试一下。

The background shows up OK if I change the background offset for Y to -44 (the height of the UISearchBar) but I can no longer type since it seems the entire text field is pushed up. I also tried to change the vertical offset for the SearchText to 44 to compensate but no luck. To be honest, I am not sure what Search Text and the Background offsets are supposed to do but I decided to give them a try.

有什么办法可以做到这一点吗?或者可能采用不同的方法?

Is there any way to accomplish this? Or maybe a different approach?

推荐答案

您可以使用

func positionAdjustmentForSearchBarIcon(_ icon: UISearchBarIcon) -> UIOffset

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

这篇关于更改UISearchBar放大图标颜色和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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