iOS 防鍵盤遮擋的實(shí)例
當(dāng)我們?cè)赨ITextField輸入數(shù)據(jù)時(shí)經(jīng)常彈出鍵盤遮擋界面,解決方法是:在彈出鍵盤時(shí)將整個(gè)UIVIew向上移動(dòng),在鍵盤消失時(shí),UIVIew還原。
實(shí)例代碼如下:
@interface ViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)UITextField* tf;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tf = [[UITextField alloc]initWithFrame:CGRectMake(10, 600, 100, 20)];
self.tf.delegate = self;
self.tf.backgroundColor = [UIColor blackColor];
[self.view addSubview:self.tf];
}
#pragma mark life Circle
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
//后臺(tái)切換到前臺(tái)通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillEnterForeground)name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[self.view endEditing:YES];
}
- (void)applicationWillEnterForeground{
[self.view endEditing:YES];
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
#pragma mark UITextFieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField{
//第一個(gè)cell不往上彈輸入框的位置
// if(indexPath.row!=0){
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
// }
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
}
#pragma mark 鍵盤操作
- (void)keyboardWillChange:(NSNotification *)note
{
NSDictionary *userInfo = note.userInfo;
CGFloat duration = [userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
CGRect keyFrame = [userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
//這個(gè)64是我減去的navigationbar加上狀態(tài)欄20的高度,可以看自己的實(shí)際情況決定是否減去;
CGFloat moveY = keyFrame.origin.y -self.tf.frame.origin.y-self.tf.frame.size.height;
NSLog(@"%f",moveY);
[UIView animateWithDuration:duration animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, moveY);
}];
}
- (void)keyboardWillHide:(NSNotification *)nsnotification
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];
[UIView animateWithDuration:0.2 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, 0);
}];
}
@end
以上這篇iOS 防鍵盤遮擋的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IOS 出現(xiàn)問題POST網(wǎng)絡(luò)請(qǐng)求狀態(tài)code:500的解決方法
這篇文章主要介紹了IOS 出現(xiàn)問題POST網(wǎng)絡(luò)請(qǐng)求狀態(tài)code:500的解決方法的相關(guān)資料,需要的朋友可以參考下2017-02-02
iOS開發(fā)中音頻視頻播放的簡單實(shí)現(xiàn)方法
視頻音頻是我們?cè)趇os日常開發(fā)中經(jīng)常會(huì)遇到的一個(gè)需求,所以下面這篇文章主要給大家介紹了關(guān)于iOS開發(fā)中音頻視頻播放的簡單實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10
iOS開發(fā)之MRC(手動(dòng)內(nèi)存管理)詳解
這篇文章主要介紹了?iOS開發(fā)之MRC(手動(dòng)內(nèi)存管理)詳解的相關(guān)資料,需要的朋友可以參考下2022-08-08
詳解iOS中Button按鈕的狀態(tài)和點(diǎn)擊事件
這篇文章先是給大家介紹iOS中Button按鈕的狀態(tài),而后又詳細(xì)介紹了iOS中按鈕點(diǎn)擊事件處理方式,本文介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-09-09
iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片詳解及實(shí)例
這篇文章主要介紹了iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10

