iOS 鍵盤遮擋 頁面上移

適配不同型號(hào)的鍵盤大小

@property (nonatomic, assign)CGSize kbSize;

viewDidLoad

[self registerForKeyboardNotifications];

各種方法


- (void)registerForKeyboardNotifications{
    
    //使用NSNotificationCenter 鍵盤出現(xiàn)時(shí)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];
    
}

- (void)keyboardWillShow:(NSNotification*)notify
{

    NSDictionary* info = [notify userInfo];
    //kbSize鍵盤尺寸 (有width, height)
    self.kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//鍵盤高度
    NSLog(@"hight_hight:%f",self.kbSize.height);

    CGRect currentFrame = self.view.frame;
    CGFloat change =self.kbSize.height;
    currentFrame.origin.y = currentFrame.origin.y - change ;

    [UIView animateWithDuration:0.00000000001 animations:^{
        self.view.frame = currentFrame;
    }];
}

///鍵盤消失事件
- (void) keyboardWillHidden {
    
    //視圖下沉恢復(fù)原狀
    [UIView animateWithDuration:0.00000000001 animations:^{
        self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
    
    [【你的TextFiled】 resignFirstResponder];

}

// 視圖將要消失時(shí),移除通知中心
-(void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1990 年8 月8 日發(fā)布1991 年4 月9 日第一次修訂1998 年8 月20 日第二次修訂2007 年4 ...
    littlelan閱讀 8,002評(píng)論 0 4
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,781評(píng)論 25 709
  • 2017-6-29 無所事事… 已經(jīng)進(jìn)化到可以當(dāng)著老師和主任的面脫白大褂走人了???♂?
    Kanyu_閱讀 231評(píng)論 0 0
  • 模版方法模式 模版方法是一種只需使用繼承就可以實(shí)現(xiàn)的非常簡(jiǎn)單的模式模版方法模式由兩部分結(jié)構(gòu)組成,第一部分是抽象父類...
    Binaryify閱讀 950評(píng)論 0 0
  • 時(shí)間以“飛”的方式前行,已不太靈活的腿,簡(jiǎn)直有點(diǎn)跟不上。鏡子里的女人容顏也不甚蒼老,只是發(fā)際的白出賣了她的年紀(jì)。 ...
    安之若零閱讀 265評(píng)論 1 0

友情鏈接更多精彩內(nèi)容