iOS中只讓textField使用鍵盤通知的實例代碼
更新時間:2017年07月19日 11:23:14 作者:弦外雨
本文通過實例代碼給大家介紹了OS中只讓textField使用鍵盤通知的操作方法,代碼簡單易懂,非常不錯,具有參考借鑒加載,需要的的朋友參考下吧
代碼:
#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增加鍵盤事件 [[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 //鍵盤事件 - (void)keyboardWillShow:(NSNotification *)notification { NSDictionary *info = [notification userInfo]; // keyboardHeight 為鍵盤高度 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 //增加鍵盤事件 -(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]; } //移除鍵盤事件 -(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使用鍵盤通知的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS App中實現(xiàn)播放音效和音樂功能的簡單示例
這篇文章主要介紹了iOS App中實現(xiàn)播放音效和音樂功能的簡單示例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03iOS UIScrollView滾動視圖/無限循環(huán)滾動/自動滾動的實例代碼
這篇文章主要介紹了iOS UIScrollView滾動視圖/無限循環(huán)滾動/自動滾動,需要的朋友可以參考下2017-02-02objective-c實現(xiàn)點到直線的距離及與垂足的交點
這篇文章主要給大家介紹了利用objective-c實現(xiàn)點到直線的距離及與垂足的交點的相關(guān)資料,文中給出了詳細(xì)的實現(xiàn)思路和實現(xiàn)代碼,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04CAMediaTiming ( 時間協(xié)議)詳解及實例代碼
這篇文章主要介紹了CAMediaTiming / 時間協(xié)議詳解及實例代碼的相關(guān)資料,這里附有實例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-12-12