当键盘弹出Xcode时,如何让uiimage和文本字段向上移动

时间:2014-07-16 21:52:32

标签: ios objective-c xcode-storyboard

我为我正在处理的应用程序创建了一个登录页面。当用户点击文本字段并弹出键盘时,我希望文本字段(密码和电子邮件)以及登录按钮向上移动。现在,当键盘弹出时,它会阻止登录按钮。当键盘向上移动时,我希望它向上移动。在Facebook messenger应用程序登录屏幕中可以看到我想要做的完美示例。

3 个答案:

答案 0 :(得分:0)

将它们放在父UIView中,然后使用动画 - 如下所示:

- (void) keyboardDidShow:(id) sender
{
    CGAffineTransform myTransform = CGAffineTransformMakeTranslation(-450.0f, 0.0f);

    [UIView animateWithDuration:0.50f
                 animations:^(void){ myView.transform = myTransform; }
                     completion:^(BOOL finished) {  } ];
}

这将导致整个视图在屏幕上向上移动450.然后,您只需要设置几个通知来触发动画(您还需要一个动画回到原位)当键盘消失时。)

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

答案 1 :(得分:0)

您希望将UIViewController.view.frame放置在键盘占用的空间量上。有几个步骤可以顺利完成。这是一些示例代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    // listen for keyboard display so we can adjust the view accordingly
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    // move the view up so that the login button is 8 points above the keyboard
    [self adjustScreenForKeyboard:notification target:self.loginButton offset:8.0f];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    // move the view back down to its original position
    [self adjustScreenForKeyboard:notification target:nil offset:0];
}

- (void)adjustScreenForKeyboard:(NSNotification *)notification target:(UIView *)target offset:(CGFloat)offset
{
    CGFloat yPos = 0;

    if (notification.name == UIKeyboardWillShowNotification) {
        // calculate how much we have to move the screen to bring the target into view
        CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
        CGFloat screenOverlap = self.view.frame.size.height - keyboardSize.height;
        CGFloat targetY = (target.frame.origin.y + target.frame.size.height + offset);

        // only set the offset if the target is actually hidden
        yPos = targetY > screenOverlap ? screenOverlap - targetY : 0;
    }
    else if (notification.name == UIKeyboardWillHideNotification) {
        // set back to original position
        yPos = 0;
    }
    else {
        // unknown notification type - do nothing
        return;
    }

    NSTimeInterval keyboardAnimationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    if (self.view.frame.origin.y == yPos) {
        return;
    }

    // slide the view up/down to coincide with the keyboard slide
    [UIView animateWithDuration:duration animations:^{
        CGRect frame = self.view.frame;
        frame.origin.y = yPos;
        self.view.frame = frame;
    }];

}

以下是需要考虑的几个要点:

  • 您真的只想将屏幕向上滑动以显示您的底部字段(例如登录按钮),而不一定是键盘本身的高度。根据屏幕(iphone5,iphone4,ipad),这将是一个不同的数量。
  • 根据语言的不同,键盘高度会有所不同,所以你想动态检测它,而不是像其他一些例子那样硬编码。
  • 您希望将幻灯片效果设置为与键盘幻灯片一致,以使其平滑。

希望有所帮助。

答案 2 :(得分:0)

之前我遇到过这个问题并以另一种方式解决了。我建议使用UITableview用表单填充表格单元格。然后当用户点击其中一个文本字段并弹出键盘时,UITableview会自动将该单元格推送到可见的位置。试试吧!

此解决方案不涉及移动单元的编码。然后,您必须关注的是该表视图的视觉方面。