0

我目前有一个表格视图控制器,并在界面构建器内的表格视图控制器顶部或上方放置了一个 UISearchDisplayController。在我的实现文件的 viewDidLoad 中,我继续将搜索栏分配给 navigationItem.titleView。我现在遇到的问题是第一个表格视图单元格中有更多空间,因为它仍然认为 searchdisplaycontroller 在那里。你可以在这里看到它:http: //imgur.com/u6cQew2

这是代码:

.h 文件

@interface searchTableViewController : UITableViewController <UISearchBarDelegate>
{
    UISearchBar *searchDrugBar;
}
@property (nonatomic, strong) IBOutlet UISearchBar *searchDrugBar;

.m 文件

- (void)viewDidLoad
{
    [super viewDidLoad];
    searchDrugBar.placeholder = @"Search for info";

    self.navigationItem.titleView = self.searchDisplayController.searchBar;
    [self.searchDisplayController setActive: YES animated: YES];
    [self.searchDisplayController.searchBar becomeFirstResponder];

}

有什么建议或想法吗?提前致谢!

4

2 回答 2

1

尝试以编程方式创建 searchBar,除非您有自己的理由,否则我看不到在情节提要中创建它有任何附加价值。

首先将其从情节提要中删除,然后:

- (void)viewDidLoad
{
    [super viewDidLoad];

    searchDrugBar = [[UISearchBar alloc] init];
    searchDrugBar.frame = CGRectMake(0, 0, 200,44); // it could be unnecessary
    searchDrugBar.delegate = self;
    searchDrugBar.placeholder = @"Search for info";

    self.navigationItem.titleView = self.searchDisplayController.searchBar;
    [self.searchDisplayController setActive: YES animated: YES];
    [self.searchDisplayController.searchBar becomeFirstResponder];

}
于 2013-10-03T03:49:38.390 回答
0
self.navigationItem.titleView = self.searchBarTop;

=>向左/右pur搜索栏:-

UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];

self.navigationItem.rightBarButtonItem = searchBarItem;

- (void)viewDidLoad
{
    ...
    self.searchDisplayController.displaysSearchBarInNavigationBar = true;
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
    ...
}

==> 这里有更多参考它可能有用我希望它对你有用..

如何在 UINavigationBar 上添加 UISearchBar 动画

于 2016-01-05T19:02:39.560 回答