ios键盘监听键盘弹出,不会档住UITextField

ios编程的时候,当一个页面有多个文本框,键盘弹出的时候会挡住底部的文本框UITextField。不能对底部的UITextField进行编辑输入。
解决办法监听键盘弹出时,算处当前键盘高度是否挡住正在编辑的文本框。如果挡住,把整个uiview的y值往上移。

第一步,定义一个全局变量记录选中的UITextField。

/**选中正在编辑的textfield*/
@property(nonatomic,strong)UITextField* selectTextField;

第二步,在viewDidLoad里面初始画你定义的所有的UITextField(这里省略了多个页面UITextField的定义)并设置多UITextField的代理,设置键盘弹出的通知。

- (void)viewDidLoad
{
    // 设置代理,记得要设置<UITextFieldDelegate>
    _contactPerson.delegate = self;
    _customerName.delegate = self;
    _province.delegate = self;
    _mobile.delegate = self;
    _city.delegate = self;
    _town.delegate = self;

    // 键盘通知
    // 键盘的frame发生改变时发出的通知(位置和尺寸)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
 }

第三步,在UITextFieldDelegate代理方法中监听当前输入的是哪个文本框。

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    //通过全局变量记录当前编辑的是哪个文本框
    _selectTextField = textField;
    return YES;
}

第四步,处理键盘通知方法。

/**
 * 键盘的frame发生改变时调用(显示、隐藏等)
 */
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    // 动画的持续时间
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    // 键盘的frame
    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];


    if (keyboardF.origin.y > self.view.height) { 
        // 键盘的Y值已经远远超过了控制器view的高度
        //键盘退下,View恢复原状
        [UIView animateWithDuration:duration animations:^{
            self.view.y = 0.0;
        }];
    }
    else
    {
        //键盘弹出,判断键盘有没有遮住当前选中的文本框,如果有遮住View往上移动,这里216是键盘高度,加60是让文本框离键盘最顶部又一段距离。
        CGFloat offset = self.view.height - (_selectTextField.y + _selectTextField.height + 216 + 60);
        if (offset <= 0) {
            [UIView animateWithDuration:duration animations:^{
                self.view.y = offset;
            }];
        }
    }
}

第五步,如果控制器销毁,要移除消息通知中心

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

当然,这里我为UIView写了一个类别(分类),上面代码才可以对View的frame直接赋值,可以直接capy用,很实用的一个分类。
实现如下:
.h文件中声明

#import <UIKit/UIKit.h>

@interface UIView (Extension)
@property (nonatomic, assign) CGFloat x;
@property (nonatomic, assign) CGFloat y;
@property (nonatomic, assign) CGFloat centerX;
@property (nonatomic, assign) CGFloat centerY;
@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;
@property (nonatomic, assign) CGSize size;
@property (nonatomic, assign) CGPoint origin;
@end

.m文件中实现(重写setter 和 getter)

#import "UIView+Extension.h"

@implementation UIView (Extension)

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin
{
    return self.frame.origin;
}
@end
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值