iOS解決呼出鍵盤遮擋頁面問題(裝逼模式一)

本文首發地址html

裝逼模式開啓—>用UITextFieldDelegate代理來解決python

鍵盤遮擋最多見的可能就是在登陸界面了,不管有多少個textfiled,不管是在VC的任何位置。都有可能形成鍵盤呼出來時,遮擋輸入框。ios

如圖1微信

firstVideo.gif

兩個TextField在VC的下部如何讓鍵盤呼出的時候恰好在你點擊TextFiled的下面呢?ide

裝逼模式代碼開始!!!學習

首先要寫上UITextFieldDelegate代理協議,,,切記!!!atom

1:添加文本輸入框spa

申明兩個屬性3d

@property (nonatomic, weak) UITextField * userNameText;

@property (nonatomic, weak) UITextField * userPwdText;

複製代碼
UITextField * userNameText = [AutolayoutView autolayoutTextFieldWithPlaceholder:@"帳號"];

userNameText.delegate = self;

[self.view addSubview:userNameText];

self.userNameText = userNameText;

UITextField * userPwdText = [AutolayoutView autolayoutTextFieldWithPlaceholder:@"密碼"];

userPwdText.delegate = self;

[self.view addSubview:userPwdText];

self.userPwdText = userPwdText;
複製代碼

2:實現代理方法代理

此處主要解決

// 當前點擊textfield的座標的Y值 + 當前點擊textFiled的高度 - (屏幕高度- 鍵盤高度 - 鍵盤上tabbar高度)

// 在這一部 就是了一個 當前textfile的的最大Y值 和 鍵盤的最全高度的差值,用來計算整個view的偏移量

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

NSLog(@"textFieldDidBeginEditing");

CGRect frame = textField.frame;

CGFloat heights = self.view.frame.size.height;

// 當前點擊textfield的座標的Y值 + 當前點擊textFiled的高度 - (屏幕高度- 鍵盤高度 - 鍵盤上tabbar高度)

// 在這一部 就是了一個 當前textfile的的最大Y值 和 鍵盤的最全高度的差值,用來計算整個view的偏移量

int offset = frame.origin.y + 42- ( heights - 216.0-35.0);//鍵盤高度216

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];

[UIView setAnimationDuration:animationDuration];

float width = self.view.frame.size.width;

float height = self.view.frame.size.height;

if(offset > 0)

{

CGRect rect = CGRectMake(0.0f, -offset,width,height);

self.view.frame = rect;

}

[UIView commitAnimations];

}
複製代碼

3:點擊空白處的時候讓其回到原來位置

/**

*  textField 取消選中狀態

*

*/

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

NSLog(@"touchesBegan");

[self.view endEditing:YES];

NSTimeInterval animationDuration = 0.30f;

[UIView beginAnimations:@"ResizeForKeyboard" context:nil];

[UIView setAnimationDuration:animationDuration];

CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);

self.view.frame = rect;

[UIView commitAnimations];

}
複製代碼

還有點擊鍵盤的return鍵的時候恢復原狀就要在

- (BOOL)textFieldShouldReturn:(UITextField *)textField;裏頭處理。
複製代碼

切記必定要判斷當前的textile是不是你點擊的self.userNameText了。在讓他恢復原狀

若有問題可添加個人QQ:1290925041 還可添加QQ羣:234812704(洲洲哥) 歡迎各位一塊學習,提升逼格! 也能夠添加洲洲哥的微信公衆號 有更多幹貨在公衆號上,請關注哦,不按期推送哦

qrcode_for_gh_30975e020db5_258.jpg
相關文章
相關標籤/搜索