iOS

来源:互联网 发布:网页编程 进销存 编辑:程序博客网 时间:2024/06/05 10:59

#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];

}


0 0
原创粉丝点击