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

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

问题描述

我有一个带有索引的UITableView;我想为它添加一个UISearchBar,但索引与x重叠以清除搜索。我已经注意到在Contacts应用程序中,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中的标题(双击选择它,然后删除)。然后,将搜索栏添加到同一视图中。将其右侧调整大小手柄向左移动,进行调整以使其与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个子视图,这就是你需要先放置视图的原因,然后是里面的NB和搜索栏。

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开发,p。 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天全站免登陆