您的位置:首页 > 产品设计 > UI/UE

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

2015-08-27 14:15 543 查看
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 "UIV
4000
iew+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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息