国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

鍵盤彈出時會覆蓋文本框怎么解決

2019-10-21 18:55:03
字體:
供稿:網(wǎng)友

在一些網(wǎng)站登陸界面,我們經(jīng)常會見到,鍵盤的出現(xiàn)與隱藏操作,那么基于代碼是如何實現(xiàn)的呢?下面小編寫了具體代碼介紹,特此分享到VEVB武林網(wǎng)平臺,供大家參考

具體代碼如下所示:

#import "ViewController.h"#import "UIView+FrameExtension.h" // 可以自己寫,以后用著方便#define kDeviceHeight [UIScreen mainScreen].bounds.size.height@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 設(shè)置視圖的背景色self.view.backgroundColor = [UIColor lightGrayColor];// 添加第一個文本框 假定位置UITextField *firstField = [[UITextField alloc]initWithFrame:CGRectMake(50, 300, 200, 40)];firstField.backgroundColor = [UIColor whiteColor];[self.view addSubview:firstField];// 添加第一個文本框UITextField *secondField = [[UITextField alloc]initWithFrame:CGRectMake(firstField.x, firstField.bottom + 50, firstField.width , firstField.height)];[self.view addSubview:secondField];secondField.backgroundColor = [UIColor whiteColor];// 注冊鍵盤顯示的通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard:) name:UIKeyboardWillShowNotification object:nil];// 注冊鍵盤隱藏的通知[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard: ) name:UIKeyboardWillHideNotification object:nil];}// 鍵盤彈出時執(zhí)行這個方法,-(void)showKeyboard:(NSNotification *)notification{// 定義一個文本框,指向正在編輯的文本框,也就是彈出鍵盤的文本框UITextField *txtField;// 今次遍歷當(dāng)前視圖的所有子視圖, subViews數(shù)組保存的是當(dāng)前視圖所有的子視圖for (UIView *subView in self.view.subviews) {// 如果這個子視圖是一個文本框的話,isKindOfClass方法可以判斷某個變量是不是某個類型的變量if ([subView isKindOfClass:[UITextField class]]) {// 先把這個子視圖轉(zhuǎn)化為文本框UITextField *tempField = (UITextField *)subView;// 再判斷這個文本框是不是正在編輯if (tempField.isEditing ) {// 如果這個文本框正在編輯,就是我要找的文本框,中斷循環(huán)txtField = tempField;break;}}}NSLog(@"%@", notification);// 獲取通知的userInfo屬性NSDictionary *userInfoDict = notification.userInfo;// 通過鍵盤通知的userInfo屬性獲取鍵盤的boundsNSValue *value = [userInfoDict objectForKey:UIKeyboardBoundsUserInfoKey];// 鍵盤的大小CGSize keyboardSize = [value CGRectValue].size;// 鍵盤高度CGFloat keyboardHeight = keyboardSize.height;CGFloat offset = kDeviceHeight - keyboardHeight - txtField.bottom ;if (offset < 0 ) { //這種情況下需要上移offset = offset - 10 ; //保存上移的高度[UIView animateWithDuration:0.5 animations:^{self.view.transform = CGAffineTransformMakeTranslation(0, offset );}];}}-(void)hideKeyboard:(NSNotification *)notification{[UIView animateWithDuration:2 animations:^{self.view.transform = CGAffineTransformIdentity;}];}// 點擊屏幕空白時隱藏鍵盤-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{[self.view endEditing:YES];}@end

關(guān)于鍵盤彈出與隱藏代碼就給大家介紹到這里,希望對大家有所幫助!


注:相關(guān)教程知識閱讀請移步到IOS開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 芮城县| 穆棱市| 嘉兴市| 牟定县| 灵寿县| 海宁市| 偃师市| 金川县| 怀仁县| 呼图壁县| 香格里拉县| 淳化县| 邵阳市| 哈尔滨市| 乐东| 西城区| 巴东县| 沙坪坝区| 安宁市| 台中市| 象州县| 鲁山县| 曲阜市| 荆州市| 旺苍县| 广东省| 鄂伦春自治旗| 准格尔旗| 通山县| 东乡族自治县| 梁河县| 武威市| 深州市| 阳朔县| 青川县| 嘉定区| 嘉善县| 虎林市| 横峰县| 嫩江县| 屏东县|