当点击取消按钮时,删除UISearchBar右侧的清除按钮(灰色x) [英] Remove clear button (grey x) to the right of UISearchBar when cancel button tapped

查看:528
本文介绍了当点击取消按钮时,删除UISearchBar右侧的清除按钮(灰色x)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,开始我的问题,这里有Spotify应用程序已经解决的问题的一些屏幕:

Right, to begin my question, here's some screenies of the problem already solved by the Spotify app:

Spotify的第1步:标准UISearchBar未处于编辑模式。

Spotify's Step 1: Standard UISearchBar not in editing mode.

第1步http://i49.tinypic .com / wbtpwi.png

Spotify的第2步: UISearchBar现在处于编辑模式。已输入搜索字词。取消按钮从右侧滑入,并出现清除按钮(灰色x)。

Spotify's Step 2: UISearchBar now in editing mode. Search term entered. Cancel button slides in from the right, and the clear button (grey x) appears.

步骤2 http://i45.tinypic.com/161kbvp.png

Spotify的第3步:取消按钮压制;键盘滑出,搜索栏不再处于编辑模式。搜索字词仍为灰色x按钮现已隐藏

Spotify's Step 3: Cancel button pressed; keyboard slides out and the search bar is no longer in editing mode. Search term remains and the grey x button is now hidden.

第3步http://i46.tinypic.com/20utv9v.png

目前,以下代码在时触发我的取消按钮被按下:

At present, the following code fires off when my cancel button is pressed:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    [searchBar setShowsCancelButton:NO animated:YES];
}

导致:

我的第3步:搜索栏现在未处于编辑模式。取消按钮和键盘已滑出。搜索词仍然存在,但灰色x也是如此。

My Step 3: Search bar now not in editing mode. Cancel button and keyboard has slid out. Search term remains but so does the grey x.

问题http:// i46.tinypic.com/rlm4w5.png

所以,我的问题是:假设 -resignFirstResponder (和 -endEditing:,FYI)当搜索栏输入文字时,会隐藏灰色x按钮,怎么办?一个隐藏吗?

So, my question is this: given that -resignFirstResponder (and -endEditing:, FYI) does not hide the grey x button when a search bar has had text entered into it, how does one hide it?

再次感谢朋友们。

推荐答案

问题是UISearchBar不公开它的文本字段,并管理文本字段本身的属性。有时,属性的值不是你想要的。

The problem is that UISearchBar doesn't expose it's text field, and manages the properties on the text field itself. Sometimes, the values of the properties aren't what you want.

例如,在我自己的应用程序中,我希望我的搜索栏的键盘样式使用透明警报样式。

For instance, in my own app, I wanted the keyboard style for my search bar to use the transparent alert style.

我的解决方案是遍历搜索栏的子视图,直到找到文本字段。然后,您应该能够设置 clearButtonMode 属性,使用类似 UITextFieldViewModeWhileEditing 作为参数。

My solution was to walk through the subviews of the search bar until you find the text field. You should then be able to set the clearButtonMode property, using something like UITextFieldViewModeWhileEditing as a parameter.

这应该使得只有在文本字段编辑时才会显示清除按钮。

This should make it so that the clear button is only shown while the text field is editing.

你想这样做 viewDidLoad 或早期的东西,所以在你开始使用它之前就已经设定了(但是在搜索栏初始化之后。

You want to do this on viewDidLoad or something early, so it's set before you start using it (but after the search bar is initialised.

for (UIView *subview in searchBar.subviews)
{
    if ([subview conformsToProtocol:@protocol(UITextInputTraits)])
    {
        [(UITextField *)subview setClearButtonMode:UITextFieldViewModeWhileEditing];
    }
}

这篇关于当点击取消按钮时,删除UISearchBar右侧的清除按钮(灰色x)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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