iOS輸入框上移

有時候UI設(shè)計的輸入框太低,我們點擊輸入框彈出鍵盤可能會擋住輸入框,所以要讓視圖上移;

代碼如下:

#pragma mark - 鍵盤高度

-(void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];

}

/*!

*? @brief? 顯示鍵盤及設(shè)置高度

*

*? @param note 通知消息-需要設(shè)置的高度

*/

-(void)keyboardShow:(NSNotification *)note

{

CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

CGFloat deltaY=keyBoardRect.size.height;

[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{

self.view.transform=CGAffineTransformMakeTranslation(0, -deltaY);

}];

}

/*!

*? @brief? 隱藏鍵盤及設(shè)置高度

*

*? @param note 通知消息-需要設(shè)置的高度

*/

-(void)keyboardHide:(NSNotification *)note

{

[UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{

self.view.transform = CGAffineTransformIdentity;

}];

}

這段代碼表示點擊輸入框,整個self.view會根據(jù)鍵盤高度上移,然后隨著鍵盤消失,self.view回到原來位置(直接復(fù)制粘貼代碼即可)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 初春四月天正好,偶得閑時與親游。 枝條綠展花朵艷,漫山遍野齊開遍。 恰想把那花香嗅,卻見蜂蟲躍而上。 游人如織皆留...
    淺徙_素簡閱讀 312評論 0 2
  • 做你沒做過的事情叫成長,做你不愿意做的事情叫改變,做你不敢做的事情叫突破。心性的強大,心量的寬廣,對生活的擔(dān)當,做...
    zhmjxx258閱讀 532評論 0 0
  • 0x00 背景 近年來,由于開發(fā)成本,開發(fā)效率,用戶需求等原因,對于移動 App 的開發(fā)方案已經(jīng)從原生開發(fā)趨向于混...
    xixizhou閱讀 9,132評論 1 6

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