iOS 防鍵盤遮擋的實(shí)例
當(dāng)我們在UITextField輸入數(shù)據(jù)時經(jīng)常彈出鍵盤遮擋界面,解決方法是:在彈出鍵盤時將整個UIVIew向上移動,在鍵盤消失時,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];
//后臺切換到前臺通知
[[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{
//第一個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];
//這個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)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
iOS如何實(shí)現(xiàn)強(qiáng)制轉(zhuǎn)屏、強(qiáng)制橫屏和強(qiáng)制豎屏的實(shí)例代碼
本篇文章主要介紹了iOS如何實(shí)現(xiàn)強(qiáng)制轉(zhuǎn)屏、強(qiáng)制橫屏和強(qiáng)制豎屏的實(shí)例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
iOS App開發(fā)中擴(kuò)展RCLabel組件進(jìn)行基于HTML的文本布局
RCLabel組件基于CoreText框架,可以將HTML標(biāo)記的文本內(nèi)容轉(zhuǎn)為富文本視圖,這里我們就來解讀如何在iOS App開發(fā)中擴(kuò)展RCLabel組件進(jìn)行基于HTML的文本布局:2016-07-07
Xcode 下刪除Provisioning Profiles文件詳細(xì)介紹
這篇文章主要介紹了Xcode 下刪除Provisioning Profiles文件詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2016-12-12
詳解iOS中多個網(wǎng)絡(luò)請求的同步問題總結(jié)
這篇文章主要介紹了詳解iOS中多個網(wǎng)絡(luò)請求的同步問題總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
解決SDK注入權(quán)限驗證安卓正常,IOS出現(xiàn)config fail的方法
這篇文章主要介紹了解決SDK注入權(quán)限驗證安卓正常,IOS出現(xiàn)config fail的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
iOS UITableView 拖動排序?qū)崿F(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了iOS UITableView 拖動排序?qū)崿F(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
IOS 開發(fā)之實(shí)現(xiàn)取消tableView返回時cell選中的問題
這篇文章主要介紹了IOS 開發(fā)之實(shí)現(xiàn)取消tableView返回時cell選中的問題的相關(guān)資料,希望通過本文能實(shí)現(xiàn)大家想要的功能,需要的朋友可以參考下2017-09-09

