首页 > 代码库 > iOS开发,使用Category实现键盘弹出时,移动View以防被遮住

iOS开发,使用Category实现键盘弹出时,移动View以防被遮住

    嗯,直接上代码!!!!

这是.h文件的

#import <UIKit/UIKit.h>@interface UIView (AboutKeyboard)@property (nonatomic) CGFloat moveDistince;@property (nonatomic) UIView *moveView;/* *指定一个View在键盘出现和消失时移动,如果存在superView则移动superView,否则移动自身 */- (void)registerWhenKeyboardShowAndHidden;/* *指定一个View在键盘出现和消失时移动,并指定要移动的View */- (void)registToMoveWhenKeyboardShowAndHideWithView:(UIView *)view;/* *取消移动 */- (void)removeRegisterWhenKeyboardShowAndHidden;@end

这是.m文件的

#import "UIView+AboutKeyboard.h"#import <objc/runtime.h>static const void *MoveDistince = &MoveDistince;static const void *MoveView = &MoveView;@implementation UIView (AboutKeyboard)@dynamic moveDistince;@dynamic moveView;- (void)setMoveDistince:(CGFloat)moveDistince{    objc_setAssociatedObject(self, MoveDistince, @(moveDistince), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (CGFloat)moveDistince{    return [objc_getAssociatedObject(self, MoveDistince) floatValue];}- (void)setMoveView:(UIView *)moveView{    objc_setAssociatedObject(self, MoveView, moveView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (UIView *)moveView{    return objc_getAssociatedObject(self, MoveView);}- (void)registerWhenKeyboardShowAndHidden{    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillShow:)                                                 name:UIKeyboardWillShowNotification                                               object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillHidden:)                                                 name:UIKeyboardWillHideNotification                                               object:nil];}- (void)registToMoveWhenKeyboardShowAndHideWithView:(UIView *)view{    self.moveView = view;    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillShow:)                                                 name:UIKeyboardWillShowNotification                                               object:nil];    [[NSNotificationCenter defaultCenter] addObserver:self                                             selector:@selector(keyboardWillHidden:)                                                 name:UIKeyboardWillHideNotification                                               object:nil];}- (void)keyboardWillShow:(NSNotification *)notification{    if (self.isFirstResponder)    {        if (!self.moveView)        {            if (self.superview)                self.moveView = self.superview;//有superView            else                self.moveView = self;//没有superView的情况下移动自身        }        NSDictionary *userInfo = notification.userInfo;        CGRect rect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];        CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];        CGRect willMoveRect = self.moveView.frame;        CGFloat selfY = CGRectGetMaxY(self.frame);        CGFloat lastY = CGRectGetHeight(willMoveRect) - CGRectGetHeight(rect);        if (duration > 0.0f)        {            if (selfY > lastY)            {                self.moveDistince = selfY - lastY;//需要移动的距离                willMoveRect.origin.y -= self.moveDistince;            }        }        else        {            CGFloat oldFloat = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size.height;            CGFloat move = (rect.size.height - oldFloat);            willMoveRect.origin.y -= move;            self.moveDistince += move;        }        [UIView animateWithDuration:duration animations:^{            self.superview.frame = willMoveRect;        }];    }}- (void)keyboardWillHidden:(NSNotification *)notification{    if (self.isFirstResponder)    {        NSDictionary *userInfo = notification.userInfo;        CGRect willMoveRect = self.moveView.frame;        willMoveRect.origin.y += self.moveDistince;        CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];        [UIView animateWithDuration:duration animations:^{            self.superview.frame = willMoveRect;        }];    }}- (void)removeRegisterWhenKeyboardShowAndHidden{    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];}@end

使用方法,以UITextField为例,如有个UITextField *textField,使用方法如下:

[textFiled registerWhenKeyboardShowAndHidden];

[textFiled registToMoveWhenKeyboardShowAndHideWithView:textField.superView];

最后,离开的时候记得

[textField removeRegisterWhenKeyboardShowAndHidden];

否则会出错。