【发布时间】:2023-03-21 06:14:01
【问题描述】:
您好,我目前在尝试使用 searchBar 隐藏工具栏时遇到问题。
我有一个带有 UIToolBar、UISearchBar(及其控制器)和 UITableView 的模态视图。 我正在使用 UIToolbar 因为这个视图实际上显示为模态视图。我想在 UINavigationController 的上下文中我想做的事情会更容易一些。
搜索时,我想隐藏工具栏。为此,当键盘出现时,我使用通知来更改组件的框架。 但是我有一个问题,我的搜索栏下有一个突出显示的空间。你可以看到截图:
http://dl.dropbox.com/u/39339665/Capture%20d%E2%80%99%C3%A9cran%202011-10-19%20%C3%A0%2016.21.43.png
我是否使用 NSNotifcationCenter 在键盘被隐藏/显示时得到通知:
- (void)viewDidLoad {
[super viewDidLoad];
[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
这是我的回调:
- (void)keyboardWillHide:(NSNotification *)notification
{
[UIView beginAnimations:nil context:nil];
CGRect toolbarFrame = self.toolBar.frame;
toolbarFrame.origin.y += toolbarFrame.size.height;
CGRect tableViewFrame = self.theTableView.frame;
tableViewFrame.origin.y += toolbarFrame.size.height;
tableViewFrame.size.height -= toolbarFrame.size.height;
self.toolBar.frame = toolbarFrame;
self.theTableView.frame = tableViewFrame;
[UIView commitAnimations];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
[UIView beginAnimations:nil context:nil];
CGRect toolbarFrame = self.toolBar.frame;
toolbarFrame.origin.y -= toolbarFrame.size.height;
CGRect tableViewFrame = self.theTableView.frame;
tableViewFrame.origin.y -= toolbarFrame.size.height;
tableViewFrame.size.height += toolbarFrame.size.height;
self.toolBar.frame = toolbarFrame;
self.theTableView.frame = tableViewFrame;
[UIView commitAnimations];
}
【问题讨论】:
标签: iphone uitableview uisearchbar uitoolbar modal-view