2

我意识到会有这样的评论是重复的:

更改 TableView 上 UISearchBars 的宽度

我认为不是。为什么我无法更改情节提要中的宽度?

在此处输入图像描述

我可以在宽度字段中写入任何值,但只要我按下返回/输入键值 320 就会恢复。

我也尝试过在视图中执行加载方法:

searchBar.frame = CGRectMake(0, 0, 222, 45);

再次零成功,有谁知道如何做到这一点?我有一个索引表,右侧的字母与搜索文本字段重叠,看起来非常难看。

更新

简而言之,这就是我想要的(概念上):

在此处输入图像描述

这是我设法制作的:

在此处输入图像描述

再次让我重申“我正在尝试调整搜索栏的大小,以便字母不会重叠”的问题。

我已经尝试过 taus-iDeveloper 的建议,它确实调整了大小,但表格单元格/部分与其重叠。

也试过这个:

更改 UISearchBar 文本字段的大小?

我设法在情节提要中调整它的大小,但是当应用程序运行时,大小没有改变。

在此处输入图像描述

我完全不知道下一步该尝试什么..

更新 2

我接受了答案,因为它有一些解决方案的要素,我得出结论,仅以编程方式使用情节提要无法做到这一点,解决方案

//Add the search bar uiview wrapper
    UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.tableView.frame.size.width, 44.0)];

    //Add the search bar and navigation item to fill in the remaining space
    UINavigationBar *remainingSearchSpace = [[UINavigationBar alloc] initWithFrame:CGRectMake(290, 0.0, (self.tableView.frame.size.width-290), 44.0)];

    searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 290, 44.0)];
    searchBar.delegate = self;
    [searchBarView addSubview:searchBar];
    [searchBarView addSubview:remainingSearchSpace];
    self.tableView.tableHeaderView = searchBarView;
4

3 回答 3

1

使用这些UISearchDisplayDeletegate函数:

- (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
于 2012-09-16T11:16:50.247 回答
1

试试这个:在 .h 文件中,

@interface SearchBarController : UIViewController <UISearchBarDelegate>

@property (nonatomic, retain) UISearchBar *mySearchBar;

在 .m 文件中,

@synthesize mySearchBar;
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = NSLocalizedString(@"SearchBarTitle", @"");

    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];    // use the table view background color

    self.mySearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 44.0)] autorelease];
    self.mySearchBar.delegate = self;
    self.mySearchBar.showsCancelButton = YES;
    self.mySearchBar.showsBookmarkButton = YES;
    [self.view addSubview: self.mySearchBar];

    // provide tint coloring and background image only if its available
    if (![self.mySearchBar respondsToSelector:@selector(setSearchFieldBackgroundImage:forState:)])
    {
        // hide the segmented control allowing for various content options
        self.contentOptions.hidden = YES;
    }
}

希望这可以帮助!!

于 2012-06-06T14:45:12.170 回答
0

您可以在搜索栏下方显示索引,而不是默认显示搜索取消按钮所在的位置。

于 2014-04-04T16:06:18.963 回答