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

首頁 > 學院 > 開發設計 > 正文

鍵盤遮擋控件(textfield/textview.......)

2019-11-14 17:57:43
字體:
來源:轉載
供稿:網友

采用的是通知的常規方式

    // 解決鍵盤遮擋問題
//選擇didShow是因為需要鍵盤的高度
//選擇willHide是因為視圖frame重置需要優先于鍵盤消失,否則表現得不連貫
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHide:) name:UIKeyboardWillHideNotification object:nil];

觸發的方法

-(void)keyboardWasShown:(NSNotification*)notification{    //獲取鍵盤高度    NSValue* value=notification.userInfo[UIKeyboardFrameBeginUserInfoKey];    CGFloat keyBoradHeight=[value CGRectValue].size.height;    NSNumber* animationTime=notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];    double time=[animationTime doubleValue];    //獲取被遮擋控件距離controller底部的距離    float a=self.view4.frame.origin.y+self.view4.frame.size.height;    float b = self.view.frame.size.height-a;    if (b<keyBoradHeight)    {        //動畫效果        [UIView animateWithDuration:time                         animations:^         {             //鍵盤被擋住了             CGRect viewCGrect=self.view.frame;             //視圖應該上移所以是-             viewCGrect.origin.y=viewCGrect.origin.y-(keyBoradHeight-b);             [self.view setFrame:viewCGrect];         } completion:nil];    }}-(void)keyboardWasHide:(NSNotification*)notification{    NSValue* value=notification.userInfo[UIKeyboardFrameBeginUserInfoKey];    CGFloat keyBoradHeight=[value CGRectValue].size.height;    float a=self.view4.frame.origin.y+self.view4.frame.size.height;    float b = self.view.frame.size.height-a;             CGRect viewCGrect=self.view.frame;         viewCGrect.origin.y=viewCGrect.origin.y+(keyBoradHeight-b);         [self.view setFrame:viewCGrect];}

最后移除通知

-(void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    [[NSNotificationCenter defaultCenter] removeObserver:self                                                    name:UIKeyboardWillHideNotification                                                  object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self                                                    name:UIKeyboardDidShowNotification                                                  object:nil];}

 最終測試改方式只適用于輸入控件比較少的界面,更多的可以使用gitHub上第三方

DaidoujiChen / DaiDodgeKeyboard

hackiftekhar / IQKeyboardManager


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 平和县| 泽普县| 哈尔滨市| 天水市| 苍山县| 万宁市| 宣汉县| 新巴尔虎右旗| 清徐县| 石柱| 丹寨县| 临武县| 泰安市| 盘锦市| 东乌| 三亚市| 五峰| 锡林浩特市| 嘉义县| 咸宁市| 天柱县| 农安县| 莱西市| 新蔡县| 枞阳县| 探索| 无极县| 潼关县| 马山县| 桃江县| 罗源县| 凌源市| 泸州市| 葫芦岛市| 科技| 沿河| 灵山县| 通许县| 长兴县| 阿鲁科尔沁旗| 洪泽县|