更改 UISearchBar TextField 的大小? [英] Changing the size of the UISearchBar TextField?

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

问题描述

我有一个带有索引的 UITableView;我想向它添加一个 UISearchBar,但索引与x"重叠以清除搜索.我注意到在联系人"应用程序中,UISearchBar 中的文本字段已调整大小以适应这一点,但我无法在我自己的应用程序中弄清楚如何执行此操作.

I have a UITableView with an Index on the side; I want to add a UISearchBar to it, but the index overlaps with the "x" to clear the search. I've noticed in the Contacts application, the textfield within the UISearchBar is resized to accommodate this, but I can't work out how to do this in my own app.

我在 viewDidLoad 中尝试了以下操作,但似乎不起作用.

I have tried the following in my viewDidLoad, but it does not seem to work.

UITextField * textField = (UITextField *)[[self.search subviews] objectAtIndex:0];
CGRect r = textField.frame;

[textField setFrame:CGRectMake(r.origin.x, r.origin.y, r.size.height, r.size.width-30)];

有什么想法吗?

推荐答案

这比所有这些建议都容易得多.在界面构建器中,不是将搜索栏作为表格视图的标题,而是可以放置一个视图.然后,在此视图中放置一个导航栏.抓住导航栏的左侧调整大小手柄并将其拉到右侧,直到 N B 只有 25 像素宽.清除 N B 中的 Title(双击选中它,然后删除).然后,在同一个视图中添加一个搜索栏.将其右侧的调整大小手柄向左移动,进行调整,使其紧靠 N B.就这样.

it's much easier than all these suggestions. In interface builder, instead of putting the Search Bar as the header of your Table View, you can put a View instead. Then, put a Navigation Bar inside this View. Grab the left resizing handle of the Navigation Bar and pull it to the right until the N B is only 25 pixels wide. Clear out the Title in the N B (double click to select it, then delete). Then, add a Search Bar into the same View. Move its right resizing handle to the left, adjust so that it abuts the N B. That's it.

如果需要,您也可以启用取消按钮,它也不会与索引重叠(保留在搜索栏中).

You can enable a cancel button if you want too and it also won't overlap the index (remains within the search bar).

显然,一个表视图的头部只能有 1 个子视图,这就是为什么你需要先把视图放在里面,然后把 N B 和搜索栏放在里面.

Apparently a Table View can only have 1 subview in its header, that's why you need to put the View first, then the N B and Search Bar inside it.

更新:请参阅 Apress 的《开始 iPhone 开发》,第 10 页.SDK 3 版本的 241.您只需在搜索时禁用索引.

UPDATE: see Beginning iPhone Development from Apress, p. 241 of SDK 3 edition. You just disable the index while searching.

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    if (isSearching) {
        return nil;
    }
    return keys;
}

他们还谈到在索引顶部添加放大镜.

Also they talk about adding a magnifying glass to the top of the index.

到处都是好书.

这篇关于更改 UISearchBar TextField 的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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