键盘弹出时会覆盖文本框怎么解决

2025-05-29 0 41

在一些网站登陆界面,我们经常会见到,键盘的出现与隐藏操作,那么基于代码是如何实现的呢?下面小编写了具体代码介绍,特此分享到快网idc平台,供大家参考

具体代码如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68
#import "ViewController.h"

#import "UIView+FrameExtension.h" // 可以自己写,以后用着方便

#define kDeviceHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 设置视图的背景色

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];

}

// 键盘弹出时执行这个方法,

-(void)showKeyboard:(NSNotification *)notification{

// 定义一个文本框,指向正在编辑的文本框,也就是弹出键盘的文本框

UITextField *txtField;

// 今次遍历当前视图的所有子视图, subViews数组保存的是当前视图所有的子视图

for (UIView *subView in self.view.subviews) {

// 如果这个子视图是一个文本框的话,isKindOfClass方法可以判断某个变量是不是某个类型的变量

if ([subView isKindOfClass:[UITextField class]]) {

// 先把这个子视图转化为文本框

UITextField *tempField = (UITextField *)subView;

// 再判断这个文本框是不是正在编辑

if (tempField.isEditing ) {

// 如果这个文本框正在编辑,就是我要找的文本框,中断循环

txtField = tempField;

break;

}

}

}

NSLog(@"%@", notification);

// 获取通知的userInfo属性

NSDictionary *userInfoDict = notification.userInfo;

// 通过键盘通知的userInfo属性获取键盘的bounds

NSValue *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

关于键盘弹出隐藏代码就给大家介绍到这里,希望对大家有所帮助!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 键盘弹出时会覆盖文本框怎么解决 https://www.kuaiidc.com/92809.html

相关文章

发表评论
暂无评论