在UISearchBar中设置取消按钮的样式 [英] Styling the cancel button in a UISearchBar
问题描述
我有一个UISearchBar有取消按钮(它显示使用 - (void)setShowsCancelButton:animated
)。我改变了搜索栏的 tintColor
,试图得到一个灰色的搜索栏:
UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,320,40)];
searchBar.tintColor = [UIColor colorWithWhite:0.8 alpha:1.0];
这是现在的样子 - 注意取消按钮也是灰色的: http://twitpic.com/c0hte
有没有办法分别设置取消按钮的颜色,使其看起来更像下面这样: http://twitpic.com/c0i6q
你想做的是很难。没有内置的钩子来取消取消按钮。
但是,如果你愿意jimmy打开引擎盖,有几个选项。
首先,UISearchBar是一个UIView,取消按钮也是一个视图,它被添加到搜索栏作为一个子视图。
我做了一些实验,可以告诉你按钮在屏幕上的大小是48,30。
所以在viewWillAppear中,你可以这样做:
-
通过查找[searchBar subviews]一个与大小48,30。 (只有似乎有一个 - 这可能会改变...)你可以仔细一点,寻找一个在大致正确的位置(不同的景观和肖像)。
-
将子视图添加到取消按钮。
-
子视图应该是UIControl(以便您可以设置enabled = NO,以确保触摸事件获取实际的取消按钮) p>
-
它需要有正确的颜色和圆角;你将需要因为我还不明白(55,30似乎工作)的原因fudge的大小
-
这将工作,如果searchBar.showsCancelButton总是是;如果您希望在不编辑搜索字符串时消失,您需要找到一个钩子,以便在每次取消按钮出现时添加。
-
你可以看到,这是一些丑陋的修补。
I have a UISearchBar that has a cancel button (it's displayed using -(void)setShowsCancelButton:animated
). I've changed the tintColor
of the search bar like this in an attempt to get a grayish searchbar:
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
searchBar.tintColor = [UIColor colorWithWhite:0.8 alpha:1.0];
This is what it looks like now - notice how the cancel button is also gray: http://twitpic.com/c0hte
Is there a way to set the color of the cancel button separately so it looks more like this: http://twitpic.com/c0i6q
What you want to do is pretty tough. There is no built-in hook to get at the cancel button.
However, there are a couple of options if you are willing to jimmy open the hood.
First off, UISearchBar is a UIView, and the Cancel button is also a view, which is added into the search bar as a subview, just as you would expect.
I have experimented a little, and can tell you that when the button is onscreen it has a size of 48,30.
So in viewWillAppear, you can do something like this:
Find the cancel button view in [searchBar subviews] by looking for one with size 48,30. (There only seems to be one -- this could change...) You could be doubly careful and look for one that is in approximately the correct position (differs in landscape and portrait).
Add a subview to the cancel button.
The subview should be a UIControl (so that you can set enabled = NO, in order to make sure touch events get to the actual cancel button)
It needs to have the right color and rounded corners; you will need to fudge the size for reasons I don't yet understand (55,30 seems to work)
This will work if searchBar.showsCancelButton is always YES; if you want it to disappear when not editing the search string, you will need to find a hook to add the overlay each time the cancel button appears.
As you can see, this is some ugly tinkering. Do it with eyes wide open.
这篇关于在UISearchBar中设置取消按钮的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!