Objective-C IOS UITextField输入框下键盘显示

今天学习ios oc 开发,发现输入框不能跟随键盘变化,通过查找资料终于解决了这个问题,具体代码如下

代码如下

前提设置

//

//  AppDelegate.h

//  uesjpad

//

//  Created by jiangfengcheng on 2022/3/12.

//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nonatomic,strong) UIWindow *window;

@end

设置如下

//

//  ViewController.m

//  uesjpad

//

//  Created by jiangfengcheng on 2022/3/12.

//

#import “ViewController.h”

@interface ViewController ()

@property (weak, nonatomic) IBOutletUITextField *username;

@property (weak, nonatomic) IBOutletUITextField *password;

@end

@implementationViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.username.placeholder=@”请输入用户名”;

    self.password.placeholder=@”请输入密码”;

    //设置密码显示

    self.password.secureTextEntry = YES;

    //方法1 通过代理来完成键盘回收

    

    //添加键盘监听事件

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(yjd_keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

}

// 输入控件键盘高度

– (void)yjd_keyboardWillShow🙁NSNotification *)notifi

{

    //获取键盘的高度

    NSDictionary *userInfo = [notifi userInfo];

    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGFloat keyboardHeight = [aValue CGRectValue].size.height;//键盘的高度

    //获取键盘动画时间

    CGFloat time = [notifi.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    //获取当前第一响应状态的输入框

    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

    UIView *view = [keyWindow performSelector:@selector(firstResponder)];

    //输入框在当前屏幕的坐标y

    CGFloat maxY =  CGRectGetMaxY([view convertRect:view.bounds toView:[[[UIApplication sharedApplication] delegate] window]]);

    //判断是非遮挡当前输入框,小于0遮挡,大于或等于0没有

    CGFloat map = [UIScreen mainScreen].bounds.size.height  – maxY – keyboardHeight;

    NSLog(@”map = %f”, map);

    if (map < 0) {

        [UIView animateWithDuration:time animations:^{

            self.view.transform = CGAffineTransformTranslate(self.view.transform, 0, map);

        }];

    }

}

//点击键盘return

– (BOOL)textFieldShouldReturn🙁UITextField *)textField {

    if (textField.returnKeyType == UIReturnKeyNext) {

        NSLog(@”textFieldShouldReturn”);

        UITextField *nextTextField = (UITextField *)[self.view viewWithTag:textField.tag + 1];

        [textField endEditing:YES];

        [nextTextField becomeFirstResponder];

    } else {

        NSLog(@”textFieldShouldReturn===transform”);

        [self endEdit];

    }

    returnYES;

}

//隐藏键盘

– (void)endEdit {

    [self.view endEditing:YES];

    self.view.transform = CGAffineTransformIdentity;

}

//点击其他位置隐藏键盘

– (void)touchesEnded🙁NSSet<UITouch *> *)touches withEvent🙁UIEvent *)event

{

    [self endEdit];

}

– (void)dealloc

{

    //移除通知

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];

}

//初始化UITextField

– (void)initTextField🙁UITextField *)textField leftImage🙁UIImage *)leftImage{

    //设置边框

    textField.layer.borderColor = [UIColor grayColor].CGColor;

    textField.layer.borderWidth = 1.0;

    textField.layer.cornerRadius = 10;

    //设置内边距、paddingTop、paddingLeft、paddingBottom、paddingRight

//    [textField setValue:[NSNumber numberWithInt:20] forKey:@”paddingLeft”];

   

    UIView *leftView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];

    

    //设置左侧“返回”

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 25, 24)];

    imageView.image =leftImage;

    [leftView addSubview:imageView];

    textField.leftView = leftView;

    

    //设置显示模式

    textField.leftViewMode = UITextFieldViewModeAlways;

    textField.clearButtonMode = UITextFieldViewModeWhileEditing;

    //设置代理

    textField.delegate = self;

}

@end

发表评论

您的邮箱地址不会被公开。 必填项已用 * 标注

滚动至顶部