iOS中只讓textField使用鍵盤通知的實(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增加鍵盤事件
[[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使用鍵盤通知的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS App中實(shí)現(xiàn)播放音效和音樂功能的簡單示例
這篇文章主要介紹了iOS App中實(shí)現(xiàn)播放音效和音樂功能的簡單示例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
iOS UIScrollView滾動(dòng)視圖/無限循環(huán)滾動(dòng)/自動(dòng)滾動(dòng)的實(shí)例代碼
這篇文章主要介紹了iOS UIScrollView滾動(dòng)視圖/無限循環(huán)滾動(dòng)/自動(dòng)滾動(dòng),需要的朋友可以參考下2017-02-02
objective-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à)值,需要的朋友們下面來一起看看吧。2017-04-04
iOS使用AFN進(jìn)行單圖和多圖上傳的實(shí)例代碼
本篇文章中主要介紹了iOS使用AFN進(jìn)行單圖和多圖上傳的實(shí)例代碼,整理出單張和多張圖片上傳的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼
這篇文章主要介紹了CAMediaTiming / 時(shí)間協(xié)議詳解及實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-12-12
Flutter?Module添加到iOS項(xiàng)目示例詳解
這篇文章主要為大家介紹了Flutter?Module添加到iOS項(xiàng)目示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
IOS程序開發(fā)之禁止輸入表情符號(hào)實(shí)例代碼
如何禁止輸入表情符號(hào)呢?下面腳本之家小編給大家分享IOS程序開發(fā)之禁止輸入表情符號(hào)實(shí)例代碼,感興趣的朋友參考下吧2016-04-04

