首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向上移动UIToolbar

向上移动UIToolbar
EN

Stack Overflow用户
提问于 2009-12-23 18:04:05
回答 5查看 8.9K关注 0票数 9

我在视图的底部有一个UIToolbar,在这个工具栏中有一个UITextField。当我开始编辑这个字段时,它隐藏在键盘后面。要查看我输入的内容,我想在显示键盘时将工具栏向上移动(然后在完成编辑后再向下移动)。

如何向上/向下移动此UIToolbar?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-12-23 20:55:52

将viewController类添加到UIKeyboardWillShowNotification/UIKeyboardWillHideNotification的观察者列表中。然后,您可以移动视图以使textView可见。您还可以从此通知中获取动画参数,以将您的动画与当前操作系统版本的键盘动画参数同步。这是我用来分页的代码

  • (void) selector:@selector(liftMainViewWhenKeybordAppears:):(BOOL)动画{超级视图viewWillAppear:动画;[NSNotificationCenter defaultCenter void selector:@selector(liftMainViewWhenKeybordAppears:):self
    • (void) name:UIKeyboardWillShowNotification object:nil];[NSNotificationCenter defaultCenter addObserver:self selector:@selector(returnMainViewToInitialposition:) name:UIKeyboardWillHideNotification object:nil];}-(空)viewWillDisappear:(BOOL)动画{超级视图viewWillAppear:动画;[NSNotificationCenter defaultCenter removeObserver:self]

在下面的方法中,我设置了两个方法来处理键盘通知。下面是这些方法:

  • (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification{ objectForKey:UIKeyboardAnimationCurveUserInfoKey objectForKey:UIKeyboardAnimationDurationUserInfoKey * userInfo = aNotification userInfo;NSTimeInterval animationDuration;UIViewAnimationCurve animationCurve;CGRect keyboardFrame;[userInfo nil:&CGRect keyboardFrame];[userInfo nil:&userInfo];[userInfo对象UIView :nil键盘边界userInfo:&keyboardFrame];UIView开始动画:nil context:nil;UIView setAnimationDuration:userInfo userInfo;UIView setAnimationCurve:animationCurve;self.view setFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y - keyboardFrame.size.height,self.view.frame.size.width,self.view.frame.size.height);UIView commitAnimations;CGRectMake

代码语言:javascript
复制
- (void) returnMainViewToInitialposition:(NSNotification*)aNotification{
    NSDictionary* userInfo = [aNotification userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardFrame;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];
    [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardFrame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
    [UIView commitAnimations];
}
票数 13
EN

Stack Overflow用户

发布于 2010-03-11 10:20:30

谢谢,它起作用了!下面是一个小小的改进:

代码语言:javascript
复制
- (void) liftMainViewWhenKeybordAppears:(NSNotification*)aNotification{
    [self scrollViewForKeyboard:aNotification up:YES];
}

- (void) returnMainViewToInitialposition:(NSNotification*)aNotification{
    [self scrollViewForKeyboard:aNotification up:NO];
}

- (void) scrollViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up{
    NSDictionary* userInfo = [aNotification userInfo];

    // Get animation info from userInfo
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardFrame;
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardBoundsUserInfoKey] getValue:&keyboardFrame];

    // Animate up or down
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];

    [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + (keyboardFrame.size.height * (up?-1:1)), self.view.frame.size.width, self.view.frame.size.height)];
    [UIView commitAnimations];
}
票数 8
EN

Stack Overflow用户

发布于 2013-10-05 04:44:36

基于上面的答案并使用方便的方法UIView animateWithDuration...观察将显示/隐藏键盘通知并使用这些处理程序。

代码语言:javascript
复制
- (void)keyboardWillShow:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey];
    NSValue *endFrame = info[UIKeyboardFrameEndUserInfoKey];

    [UIView animateWithDuration:durationValue.doubleValue
                          delay:0
                        options:(curveValue.intValue << 16)
                     animations:^{
                         self.navigationController.toolbar.frame = CGRectMake(0,
                                                                              [endFrame CGRectValue].origin.y - self.navigationController.toolbar.bounds.size.height,
                                                                              self.navigationController.toolbar.bounds.size.width,
                                                                              self.navigationController.toolbar.bounds.size.height);
                     }
                     completion:nil];
}

- (void)keyboardWillHide:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    NSNumber *durationValue = info[UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curveValue = info[UIKeyboardAnimationCurveUserInfoKey];

    [UIView animateWithDuration:durationValue.doubleValue
                          delay:0
                        options:(curveValue.intValue << 16)
                     animations:^{
                         self.navigationController.toolbar.frame = CGRectMake(0,
                                                                              self.view.bounds.size.height - self.navigationController.toolbar.bounds.size.height,
                                                                              self.navigationController.toolbar.bounds.size.width,
                                                                              self.navigationController.toolbar.bounds.size.height);
                     }
                     completion:nil];
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1951826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档