iOS中只讓textField使用鍵盤(pán)通知的實(shí)例代碼
代碼:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //為textField增加鍵盤(pán)事件 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addKeyboardNoti) name:UITextFieldTextDidBeginEditingNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeKeyboardNoti) name:UITextFieldTextDidEndEditingNotification object:nil]; } #pragma -mark -keyboard notificatin //鍵盤(pán)事件 - (void)keyboardWillShow:(NSNotification *)notification { NSDictionary *info = [notification userInfo]; // keyboardHeight 為鍵盤(pán)高度 CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; [self animateViewWithKeyboardHeight:keyboardSize.height]; } - (void)keyboardWillHide:(NSNotification *)notification { [self animateViewWithKeyboardHeight:0.0]; } - (void)animateViewWithKeyboardHeight:(CGFloat)keyboardHeight { NSTimeInterval animationDuration = 0.3f; CGFloat height = self.view.bounds.size.height; CGFloat width = self.view.bounds.size.width; CGFloat topSize = 0.0; CGFloat viewH = self.view.frame.size.height-64; CGFloat deviceHeight = [UIScreen mainScreen].bounds.size.height; CGFloat animateH = deviceHeight - viewH - keyboardHeight; if (animateH >= 0) { topSize = 0; CGRect toRect = CGRectMake(0, topSize, width, height); self.view.frame = toRect; } else { topSize = animateH; CGRect toRect = CGRectMake(0, topSize, width, height); [UIView animateWithDuration:animationDuration animations:^{ self.view.frame = toRect; }]; } } #pragma -mark -UITextFieldText Notification //增加鍵盤(pán)事件 -(void)addKeyboardNoti { NSLog(@"------addKeyboardNoti-------"); [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } //移除鍵盤(pán)事件 -(void)removeKeyboardNoti { NSLog(@"------removeKeyboardNoti---------"); [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
總結(jié)
以上所述是小編給大家介紹的iOS中只讓textField使用鍵盤(pán)通知的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- iOS應(yīng)用開(kāi)發(fā)中監(jiān)聽(tīng)鍵盤(pán)事件的代碼實(shí)例小結(jié)
- iOS中的UIKeyboard鍵盤(pán)視圖使用方法小結(jié)
- iOS實(shí)現(xiàn)輸入框跟隨鍵盤(pán)自動(dòng)上移的實(shí)例代碼
- iOS中鍵盤(pán) KeyBoard 上添加工具欄的方法
- iOS項(xiàng)目開(kāi)發(fā)鍵盤(pán)彈出遮擋輸入框問(wèn)題解決方案
- h5 ios輸入框和鍵盤(pán)的兼容性?xún)?yōu)化指南
- iOS開(kāi)發(fā)第三方鍵盤(pán)處理實(shí)例代碼
- iOS自定義鍵盤(pán)切換效果
- IOS數(shù)字鍵盤(pán)左下角添加完成按鈕的實(shí)現(xiàn)方法
- iOS自定義身份證鍵盤(pán)
相關(guān)文章
iOS App中實(shí)現(xiàn)播放音效和音樂(lè)功能的簡(jiǎn)單示例
這篇文章主要介紹了iOS App中實(shí)現(xiàn)播放音效和音樂(lè)功能的簡(jiǎn)單示例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03iOS UIScrollView滾動(dòng)視圖/無(wú)限循環(huán)滾動(dòng)/自動(dòng)滾動(dòng)的實(shí)例代碼
這篇文章主要介紹了iOS UIScrollView滾動(dòng)視圖/無(wú)限循環(huán)滾動(dòng)/自動(dòng)滾動(dòng),需要的朋友可以參考下2017-02-02objective-c實(shí)現(xiàn)點(diǎn)到直線的距離及與垂足的交點(diǎn)
這篇文章主要給大家介紹了利用objective-c實(shí)現(xiàn)點(diǎn)到直線的距離及與垂足的交點(diǎn)的相關(guān)資料,文中給出了詳細(xì)的實(shí)現(xiàn)思路和實(shí)現(xiàn)代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-04-04iOS使用AFN進(jìn)行單圖和多圖上傳的實(shí)例代碼
本篇文章中主要介紹了iOS使用AFN進(jìn)行單圖和多圖上傳的實(shí)例代碼,整理出單張和多張圖片上傳的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼
這篇文章主要介紹了CAMediaTiming / 時(shí)間協(xié)議詳解及實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-12-12Flutter?Module添加到iOS項(xiàng)目示例詳解
這篇文章主要為大家介紹了Flutter?Module添加到iOS項(xiàng)目示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08IOS程序開(kāi)發(fā)之禁止輸入表情符號(hào)實(shí)例代碼
如何禁止輸入表情符號(hào)呢?下面腳本之家小編給大家分享IOS程序開(kāi)發(fā)之禁止輸入表情符號(hào)實(shí)例代碼,感興趣的朋友參考下吧2016-04-04