简体   繁体   English

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

[英]Change UISearchBar magnify icon color and position

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.我有一个 UISearchBar,我想更改初始放大图标的位置(出现在 UISearchBar 中间的那个)以及颜色或图标。

So far I changed the tint and the icon image.到目前为止,我更改了色调和图标图像。

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

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

As for the magnify icon position, I want it in the left side, where it shows up if I activate the UISearchBar.至于放大图标的位置,我希望它在左侧,如果我激活 UISearchBar,它就会显示在那里。

在此处输入图片说明

在此处输入图片说明

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.我在这里找到了很多答案,许多提供的解决方案不适用于 iOS 8.0+ 或在 Objective-C 中,我在理解它们时遇到了一些问题。

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:我试图添加一个包含图标的自定义背景,但它显示在搜索栏的底部,而图标仍然存在:

在此处输入图片说明

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.如果我将 Y 的背景偏移更改为 -44(UISearchBar 的高度),背景显示正常,但我无法再输入,因为似乎整个文本字段都被向上推了。 I also tried to change the vertical offset for the SearchText to 44 to compensate but no luck.我还尝试将 SearchText 的垂直偏移更改为 44 以进行补偿,但没有运气。 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?或者也许是一种不同的方法?

@Michael - Thanks for all the help. @Michael - 感谢所有帮助。 I managed to grab the UISearchBar's UITextField:我设法抓住了 UISearchBar 的 UITextField:

func customizeSearchBar()
{
    for subview in srcRegimenSearchBar.subviews
    {
        for view in subview.subviews
        {
            if let searchField = view as? UITextField
            {
                let imageView = UIImageView()
                let image = UIImage(named: "SearchBarIcon.png")

                imageView.image = image;
                imageView.frame = CGRectMake(0, 0, 0, 0)

                /*imageView.frame = CGRectMake(100, 0, 20, 19)*/

                searchField.leftView = imageView
                searchField.leftViewMode = UITextFieldViewMode.Always
            }
        }
    }
}

I wanted to change the position since I have access to the frame but it seems only the the top and height can be modified, at least the way I tried so I set the height and width to 0 (I couldn't find a way to make it nil or remove it completely) and I added an UIImageView with the new icon over in the left side of the UISearchbar and added a custom horizontal offset for the tint.我想更改位置,因为我可以访问框架,但似乎只能修改顶部和高度,至少是我尝试的方式,因此我将高度和宽度设置为 0(我找不到方法使其为零或完全删除),我在 UISearchbar 的左侧添加了一个带有新图标的 UIImageView,并为色调添加了自定义水平偏移。

Not the best solution out there, I'm sure of it, but for now it works.不是最好的解决方案,我敢肯定,但现在它有效。

在此处输入图片说明

在此处输入图片说明

You can use:您可以使用:

uiSearchBar.setPositionAdjustment(UIOffset, for: UISearchBar.Icon)

replace UIOffset with your desired offset value UIOffset(horizontal: CGFloatvertical: CGFloat) and UISearchBar.Icon with .search替换UIOffset与期望的偏移值UIOffset(horizontal: CGFloatvertical: CGFloat)UISearchBar.Icon.search

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM