UISearchBar自定义角落 [英] UISearchBar custom corners

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

问题描述

我正在尝试创建一个这样的搜索栏:

I'm trying to create a search bar like this:

但我注意到我可能不得不用我自己的图像替换搜索栏,因为搜索当我设置时,条形角出错了:

But I'm noticing that I'm probably going to have to replace the search bar with my own image because the search bar corners comes out wrong when I set:

self.searchController.searchBar.layer.cornerRadius = 50 // I've tried other numbers besides this too with no luck
self.searchController.searchBar.clipsToBounds = true

如果我这样设置:

self.searchController.searchBar.layer.cornerRadius = self.searchController.searchBar.bounds.height/2

搜索栏如下所示:

哪个是直到与图像中的不完全相同。

Which still isn't exact like in the image.

有没有办法用图像替换文本字段的左侧和右侧,这样我就可以使用圆角从我的自定义搜索栏?

Is there a way to replace the left and right side of the textfield with an image that way I can use the rounded corners from my custom search bar?

推荐答案

这里的问题是你在UISearchBar上设置了角半径,而不是它里面的UITextField 。你可以做一些黑客来获取UITextField,但这并不是真的推荐。

The issue here is you are setting the corner radius on the UISearchBar, not the UITextField inside it. You can do some sort of hack to get the UITextField, but that's not really recommended.

正如你在问题中提到的,你需要使用自定义图像和此处显示的方法: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/doc/uid/TP40007529-CH3-SW40

As you mentioned in your question, you'll need to use custom images and the methods shown here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchBar_Class/#//apple_ref/doc/uid/TP40007529-CH3-SW40

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

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