gpt4 book ai didi

iphone - 除非在 TableView 内,否则无法显示 UISearchBar

转载 作者:行者123 更新时间:2023-12-03 19:11:21 25 4
gpt4 key购买 nike

我的目标是将 UISearchBar 固定在 UITableView 正上方的 View 中。当我在 IB 中进行设置然后构建时,表格 View 会展开以填充整个窗口,并且搜索栏不可见。如果我将 UISearchBar 作为 UITableView 的 subview ,搜索栏将按预期显示,但这不是我想要的。我想要的是,在用户选择一行后,我想显示一个详细 View ,搜索栏仍保留在屏幕上。所以我认为它需要是一个单独的 subview ,而不是表的一部分。但出于某种原因,当搜索栏只是表格外部的 subview 时,我无法显示搜索栏。

最佳答案

您必须以编程方式执行此操作。这就是我最终所做的。首先将以下内容添加到 ViewController 的 .h 文件中。

@interface YourViewController : UITableViewController <UISearchBarDelegate> 
{
UISearchBar *mySearchBar;
}
@property (nonatomic, retain) UISearchBar *mySearchBar;
@end

然后将此代码放入我的 RootViewController 的 viewDidLoad 方法中。

    self.mySearchBar = [[[UISearchBar alloc] 
initWithFrame:CGRectMake(0.0, 64.0, self.view.bounds.size.width,
44.0)] autorelease];
self.mySearchBar.delegate = self;
self.mySearchBar.showsCancelButton = YES;
self.mySearchBar.hidden = YES;
self.mySearchBar.tintColor = [UIColor lightGrayColor];
[self.navigationController.view addSubview: self.mySearchBar];

您可能还需要添加类似的内容以防止 searchBar 位于 tableView 之上。

    self.tableView.frame = CGRectMake(0.0, 44.0, 320, 324);

就我而言,当我深入到详细 View 时,搜索栏仍保留在 View 中。我不得不打电话:

    self.mySearchBar.hidden = YES;`

在我的 didSelectRowAtIndexPath 中,当我单击单元格时将其删除。

关于iphone - 除非在 TableView 内,否则无法显示 UISearchBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1263355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com