iOS8上:这是怎么回事时用键盘移动过渡看法? [英] iOS8: What's going on with moving views during keyboard transitions?

查看:184
本文介绍了iOS8上:这是怎么回事时用键盘移动过渡看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到iOS8上之后,我越来越怪异的行为,当我一个键盘的过渡期间移动的看法。任何人能解释这是怎么回事?

下面是一个小例子来说明问题。我有一个的UITextField 的UIButton 一个简单的观点。功能 nudgeUp 10点移动文本框和按钮了。它可触发pssed的按钮$ P $ 回调,或 keyboardWillShow 回调。

当我点击该按钮时,code按预期工作:按钮pressed 通话 nudgeUp 和按钮和文本字段10点跳起来。

当我点击文本字段, keyboardWillShow 要求 nudgeUp ,但行为是非常不同的。该按钮和文本字段立即跳转的的10分,然后滑动恢复到原来的位置,键盘显示本身。

这是怎么回事?我怎样才能重新获得iOS8上键​​盘presentation在动画的控制?

 #进口ViewController.h@implementation的ViewController - (无效)viewDidLoad中{
    [超级viewDidLoad中];
    [NSNotificationCenter defaultCenter]的addObserver:自我
                                             选择:@选择(keyboardWillShow :)
                                                 名称:UIKeyboardWillShowNotification
                                               对象:无];} - (无效)keyboardWillShow:(NSNotification *)的通知
{
    //键盘出现时调用。
    [个体经营nudgeUp]
} - (IBAction为)按钮pressed:(ID)发送{
    [个体经营nudgeUp]
} - (无效)nudgeUp
{
    的CGRect newTextFieldFrame = self.textField.frame;
    newTextFieldFrame.origin.y - = 10;
    self.textField.frame = newTextFieldFrame;    的CGRect newButtonFrame = self.button.frame;
    newButtonFrame.origin.y - = 10;
    self.button.frame = newButtonFrame;
}
@结束


解决方案

这是自动版式。东西iOS8上改变,你不能只是改变框架或中心点了,如果你启用了自动版式。你必须创建约束(垂直空间)的出口(S)和更新相应而不是改变画面位置。约束是像任何其他UI控制,并且可以具有一个出口。约束的变化可以是动画。

例如:

  [UIView的animateWithDuration:[notification.userInfo [UIKeyboardAnimationDurationUserInfoKey]的doubleValue]延迟:0选项:[[[通知用户信息] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue]动画:^ {
    self.bottomSpaceConstraint.constant = adjustmentedValue;
    [self.view layoutIfNeeded]
}完成:^(BOOL完){
}];

After switching to iOS8, I'm getting weird behavior when I move views during a keyboard transition. Can anyone explain what's going on?

Here's a minimal example to demonstrate the problem. I have a simple view with a UITextField and a UIButton. The function nudgeUp moves the text field and the button up by 10 points. It is triggered either by the buttonPressed callback, or the keyboardWillShow callback.

When I tap the button, the code works as expected: buttonPressed calls nudgeUp and the button and text field jump up by 10 points.

When I tap the text field, keyboardWillShow calls nudgeUp, but the behaviour is very different. The button and text field immediately jump down by 10 points, and then slide back up to their original position as the keyboard shows itself.

Why is this happening? How can I regain control of animations during keyboard presentation in iOS8?

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

}

- (void)keyboardWillShow:(NSNotification *)notification
{
    // Called when the keyboard appears.
    [self nudgeUp];
}

- (IBAction)buttonPressed:(id)sender {
    [self nudgeUp];
}

- (void)nudgeUp
{
    CGRect newTextFieldFrame = self.textField.frame;
    newTextFieldFrame.origin.y -= 10;
    self.textField.frame = newTextFieldFrame;

    CGRect newButtonFrame = self.button.frame;
    newButtonFrame.origin.y -= 10;
    self.button.frame = newButtonFrame;
}
@end

解决方案

It's AutoLayout. Something changed in iOS8 and you can't just change frame or center points anymore if you have AutoLayout enabled. You have to create an outlet(s) of your constraint (vertical space) and update it accordingly instead of changing frame position. Constraints are like any other ui control and can have an outlet. Constraint change can be animated.

Example:

[UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{        
    self.bottomSpaceConstraint.constant = adjustmentedValue;
    [self.view layoutIfNeeded];        
} completion:^(BOOL finished) {
}];

这篇关于iOS8上:这是怎么回事时用键盘移动过渡看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆