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í)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS密碼在進(jìn)入后臺(tái)1小時(shí)后重新設(shè)置
這篇文章主要介紹了iOS密碼在進(jìn)入后臺(tái)1小時(shí)后重新設(shè)置的相關(guān)資料,需要的朋友可以參考下2017-08-08
ios uicollectionview實(shí)現(xiàn)橫向滾動(dòng)
這篇文章主要為大家詳細(xì)介紹了ios uicollectionview實(shí)現(xiàn)橫向滾動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
舉例講解iOS應(yīng)用開發(fā)中對設(shè)計(jì)模式中的策略模式的使用
這篇文章主要介紹了iOS應(yīng)用設(shè)計(jì)中對設(shè)計(jì)模式中的策略模式的使用,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03
iOS實(shí)現(xiàn)毫秒倒計(jì)時(shí)的方法詳解
倒計(jì)時(shí)在我們?nèi)粘i_發(fā)中必不可少,最近在公司的一個(gè)項(xiàng)目中就遇到了這個(gè)需求,本文著重介紹的是利用iOS實(shí)現(xiàn)毫秒倒計(jì)時(shí)的方法,文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-04-04
iOS UIWebView 通過 cookie 完成自動(dòng)登錄實(shí)例
本篇文章主要介紹了iOS UIWebView 通過 cookie 完成自動(dòng)登錄實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
iOS 修改alertViewController彈框的字體顏色及字體的方法
下面小編就為大家分享一篇iOS 修改alertViewController彈框的字體顏色及字體的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

