美文网首页
textField随键盘上下移动

textField随键盘上下移动

作者: March_Cullen | 来源:发表于2017-03-02 20:40 被阅读0次
#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随键盘上下移动

      本文链接:https://www.haomeiwen.com/subject/uwvkgttx.html