首页 > 系统 > iOS > 正文

iOS - 通知监听键盘高度

2019-11-06 09:58:41
字体:
来源:转载
供稿:网友

#define   SINGLE_NOTICE   [NSNotificationCenter defaultCenter]

#import "ViewController.h"

@interface ViewController ()<UITextFieldDelegate> {

    float _keyboardHeight; // 键盘高度

}

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {

    [superviewDidAppear:animated];

    [SINGLE_NOTICEaddObserver:selfselector:@selector(keyBoardWillShown:)name:UIKeyboardWillChangeFrameNotificationobject:nil];

}

- (void)viewDidDisappear:(BOOL)animated {

    [superviewDidDisappear:animated];

    [SINGLE_NOTICEremoveObserver:selfname:UIKeyboardWillChangeFrameNotificationobject:nil];

}

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColorwhiteColor];

    

    for (NSInteger i =0; i<2; i++) {

        // 方便对比,创建两个输入框

        UITextField *tf = [[UITextFieldalloc] init];

        tf.frame =CGRectMake(50,500-300*i,200, 40);

        tf.backgroundColor = [UIColorlightGrayColor];

        tf.placeholder =@"input your info at this...";

        tf.delegate =self;

        [tf setValue:[UIColorredColor] forKeyPath:@"_placeholderLabel.textColor"];

        [self.viewaddSubview:tf];

    }

}

#PRagma mark - 通知方法

- (void)keyBoardWillShown:(NSNotification *)noti {

    NSDictionary *info = [notiuserInfo];

    

    NSValue *value = [infoobjectForKey:UIKeyboardFrameBeginUserInfoKey];

    

    // 字典,含有键盘弹出时间,弹出高度的字典

    // NSLog(@"info===>>>%@",info);

    

    CGRect keyboardRect = [valueCGRectValue];

    

    float keyboardHeight = keyboardRect.size.height;

    

    _keyboardHeight = keyboardHeight;

    

    // 键盘高度

  //  NSLog(@"keyHeight===%f",keyboardHeight);

    

    /** 控制台输出结果 (iphone 7plus模拟器)

     

     2017-02-27 14:13:01.260 InputKeyboardHeight[1205:253104] info===>>>{

     UIKeyboardAnimationCurveUserInfoKey = 7;

     UIKeyboardAnimationDurationUserInfoKey = "0.25";

     UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {414, 271}}";

     UIKeyboardCenterBeginUserInfoKey = "NSPoint: {207, 871.5}";

     UIKeyboardCenterEndUserInfoKey = "NSPoint: {207, 600.5}";

     UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 736}, {414, 271}}";

     UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 465}, {414, 271}}";

     UIKeyboardIsLocalUserInfoKey = 1;

     }

     

     2017-02-27 14:13:01.260 InputKeyboardHeight[1205:253104] keyHeight===271.000000

     */

    

}

#pragma mark - 输入框代理方法 -

- (void)textFieldDidBeginEditing:(UITextField *)textField {

    

    if (_keyboardHeight ==0) {

        // 安全判断

        _keyboardHeight =271.0f;

    }

    

    // 输入框往上面提的高度

    float textFieldUpHeight =_keyboardHeight - (self.view.frame.size.height - (textField.frame.size.height+textField.frame.origin.y));

    

    if (textFieldUpHeight >0) {

        // 接收到键盘高度改变之后,做的事情...

        [UIViewanimateWithDuration:0.25animations:^{

            

            self.view.frame =CGRectMake(0, -textFieldUpHeight,self.view.frame.size.width,self.view.frame.size.height);

            

        }];

    }

}

- (void)textFieldDidEndEditing:(UITextField *)textField {

    [UIViewanimateWithDuration:0.25animations:^{

        

        self.view.frame =CGRectMake(0,0, self.view.frame.size.width,self.view.frame.size.height);

        

    }];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [self.viewendEditing:YES];

}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表