3

我正在尝试创建一个简单的模式视图控制器,让您可以使用文本视图编辑文本。但是,当我以模态方式呈现视图控制器时,它会从左下方向滑入,而不是仅从底部滑入。

这是一段奇怪效果的视频:http: //youtu.be/9M_MHA5mt1M

我的控制器只是监视键盘显示,然后适当地使用自动布局调整文本视图的大小。这是代码:

#import "TextPicker.h"

@interface TextPicker ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *keyboardHeight;

@end

@implementation TextPicker

- (id)initWithText:(NSString *)text
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    self = [storyboard instantiateViewControllerWithIdentifier:@"textPicker"];
    if (self) {
        self.text = text;

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self observeKeyboard];

    //self.textView.text = self.text;
    [self.textView becomeFirstResponder];
}


- (void) viewWillDisappear:(BOOL)animated {
    [self.textView resignFirstResponder];
}

- (IBAction)savePressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];    
}

- (IBAction)cancelPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void) dealloc {
    [self stopObervingKeyboard];
}

- (void)observeKeyboard {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)stopObervingKeyboard {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];

    NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardFrame = [kbFrame CGRectValue];
    self.keyboardHeight.constant = -keyboardFrame.size.height;

    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:animationDuration animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];
    NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    self.keyboardHeight.constant = 0;
    [UIView animateWithDuration:animationDuration animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (IBAction)dismissKeyboard:(id)sender {
    [self.textView resignFirstResponder];
}


@end
4

1 回答 1

5

[self.view layoutIfNeeded]通过将调用包装在动画块中,您的视图正在动画化。

viewDidLoad您开始观察键盘变化时,当您检测到它们时,您会为调整设置动画,这通常是正确的。但是,在视图进行第一次布局之前,您会显示键盘;这会导致所有视图的动画从CGRectZero它们的适当大小。这就是您所看到的效果。

所以基本上你需要在你的动画调用之前给视图一个布局的机会。layoutIfNeeded可能最简单的方法是简单地移动[self.textView becomeFirstResponder];viewWillAppear:viewDidAppear:

*作为旁注,请记住在外观呼叫中呼叫超级。我注意到你没有打电话[super viewWillDisappear];

于 2013-03-09T05:49:53.780 回答