国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > 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];

}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰台区| 镇巴县| 东乌珠穆沁旗| 习水县| 宜川县| 双鸭山市| 平南县| 玉环县| 兴义市| 盱眙县| 赞皇县| 乌拉特后旗| 乐都县| 莎车县| 高尔夫| 海淀区| 惠州市| 泗洪县| 买车| 黔江区| 邓州市| 深水埗区| 山阴县| 博罗县| 永昌县| 师宗县| 抚宁县| 镇康县| 胶南市| 新昌县| 教育| 临漳县| 上饶县| 资溪县| 樟树市| 灌阳县| 郸城县| 鹤壁市| 山西省| 蕉岭县| 泾川县|