使用键盘移动 UIToolbar iOS8

Move UIToolbar with keyboard iOS8

我的 ViewController 底部有一个 UIToolbar,它继承自 self.navigationController。我在 ViewController 中使用以下代码在 viewWillAppear: 方法中显示它:

[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。每当我的 UITextView 开始编辑时,我希望整个 UIToolbar 移动到键盘的最顶部。

为了完成这个,我有 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?

此外,如果可能的话,我怎样才能简单地使这段代码实现我的目标而不会使事情复杂化并制作一个编程工具栏,我宁愿坚持使用固有的工具栏。谢谢。

第一个:

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)如果您有自己的工具栏(制作 programmatically/customed)..