textField随键盘上下移动

#import "LYViewController.h"

@interface LYViewController ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomSpace;
@end

@implementation LYViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"评论";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"commend_detail_top_forward_normal.9" highLightedImage:@"commend_detail_top_forward_pressed.9" target:self action:@selector(btnClick:)];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
}

- (void)btnClick:(UIButton *)btn {
}

- (void)keyboardWillChangeFrame:(NSNotification *)note {
    // 键盘显示或者隐藏完毕的frame
    CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 修改底部约束
    self.bottomSpace.constant = KScreenHeight - frame.origin.y;
    // 动画时间
    CGFloat duration = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

你可能感兴趣的:(textField随键盘上下移动)