我的 ViewController 底部有一个 UIToolbar ,它继承自self.navigationController
。我使用此代码在viewWillAppear:
方法的 ViewController 中显示它:
[self.navigationController setToolbarHidden:NO animated:NO];
self.navigationController.toolbar.backgroundColor = [UIColor blackColor];
self.navigationController.toolbar.tintColor = [UIColor blackColor];
self.navigationController.toolbar.barStyle = UIBarStyleBlackOpaque;
之后,我的 ViewController 以以编程方式添加了 UITextView 和 UIButton ,这将允许用户添加注释到 ViewController ,实际上是一个 UITableViewController 。我希望整个 UIToolbar 在我的 UITextView 开始编辑时移动到键盘的顶部。
要做到这一点,我有A)
- 在viewDidLoad:
方法中将以下观察者添加到我的 ViewController
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
和B)
- 增加了以下方法来控制navigationController固有的UIToolbar的重新定位。
- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary* keyboardInfo = [aNotification userInfo];
NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameEndRect = [keyboardFrameEnd CGRectValue];
// the keyboard is showing so resize the table's height
NSTimeInterval animationDuration =
[[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame; //self.view.frame
self.navigationController.toolbar = CGRectMake(self.navigationController.toolbar.frame.origin.x,
self.navigationController.toolbar.frame.origin.y + 10,
self.navigationController.toolbar.frame.size.width,
self.navigationController.toolbar.frame.size.height);
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)aNotification
{
NSDictionary* keyboardInfo = [aNotification userInfo];
NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardFrameEndRect = [keyboardFrameEnd CGRectValue];
// the keyboard is hiding reset the table's height
NSTimeInterval animationDuration =
[[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect frame = self.view.frame;
frame.origin.y += self.navigationController.toolbar.frame.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
self.view.frame = frame;
[UIView commitAnimations];
}
我已经尝试了各种方法来尝试使其工作,我发现这种方法对我的应用程序来说是最简单的。我收到了错误:
Assignment to read-only property
在这一行:
self.navigationController.toolbar = CGRectMake(self.navigationController.toolbar.frame.origin.x,
self.navigationController.toolbar.frame.origin.y + 10,
self.navigationController.toolbar.frame.size.width,
self.navigationController.toolbar.frame.size.height);
keyboardWillShow:
方法中的。看来我无法改变navigationController固有工具栏的位置,这是否意味着我需要以编程方式创建我的UIToolbar?
另外,如果可能的话,我怎样才能简单地让这个代码实现我的目标而不会使事情复杂化并制作一个编程工具栏,我宁愿坚持使用固有的工具栏。感谢。
答案 0 :(得分:1)
首先:
self.view没有工具栏,你必须单独移动工具栏..
第二
self.navigationController.toolbar = CGRectMake..
这是错误的,self.navigationController.toolbar.frame = CGRectMake..
是正确的方法。
第三
使用self.navigationController.toolbar.frame.origin.y - keyboardFrame.size.height
我认为您必须根据键盘的高度正确计算self.navigationController.toolbar
的帧,并为其设置动画。
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:animationDuration];
[self.navigationController.toolbar setFrame:CGRectMake(self.navigationController.toolbar.frame.origin.x,
self.navigationController.toolbar.frame.origin.y - YourKeyboardFrame.size.height,
self.navigationController.toolbar.frame.size.width,
self.navigationController.toolbar.frame.size.height)];
[UIView commitAnimations];
另一个选项是使用UITextFields -inputAccessoryView
(如果您正在使用UITextField),如果您有自己的工具栏(以编程方式/定制)...