美文网首页
OC 解决键盘遮盖问题,可直接拷贝使用

OC 解决键盘遮盖问题,可直接拷贝使用

作者: 木马sun | 来源:发表于2019-05-29 10:45 被阅读0次

直接上代码
.h 文件

@interface KeyBoderHandlerTool : NSObject

- (void)handleKeyBoderAction:(UIView *)handleView  offsetY:(CGFloat )offsetY;
+ (instancetype)getSharedKeyBoderTool;
@end

.m文件

#import "KeyBoderHandlerTool.h"


@interface KeyBoderHandlerTool ()

@property (strong, nonatomic) NSArray   *names;
@property (strong, nonatomic) UIView *keyBoderHandleView;
@property (assign, nonatomic) CGFloat keyBoderHandleHeigt;

@property (strong, nonatomic) KeyBoderHandlerTool *sharedTool;


@end

static KeyBoderHandlerTool *sharedTool = nil;
@implementation KeyBoderHandlerTool

+ (instancetype)getSharedKeyBoderTool{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedTool = [self new];
    });
    return sharedTool;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedTool = [super allocWithZone:zone];
    });
    return sharedTool;
}

/**
 处理键盘遮盖

 @param handleView 可能会被遮盖的子视图
 @param offsetY 如果父视图是可滑动视图,则 需传入在Y 轴上的 偏移量, 否则传 0
 */
- (void)handleKeyBoderAction:(UIView *)handleView  offsetY:(CGFloat )offsetY{
    
    if (self.keyBoderHandleView != nil) {
        self.keyBoderHandleView = handleView;
        return;
    }
    
    self.keyBoderHandleView = handleView;
    
    
    __weak KeyBoderHandlerTool *weakSelf = self;
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardDidShowNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        [weakSelf keyboardDidShowFrame:note offsetY:offsetY];
    }];
    
    
    
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillHideNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
        [weakSelf keyboardWillHidden:note];
    }];
    
}


//键盘出现
- (void)keyboardDidShowFrame:(NSNotification *)note offsetY:(CGFloat )offsetY{
    
    // 取出键盘最终的frame
    CGRect keyBoderRect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    // 取出键盘弹出需要花费的时间
    double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    CGFloat boundHeight = UIScreen.mainScreen.bounds.size.height;
   
    CGFloat viewMaxY = CGRectGetMaxY(self.keyBoderHandleView.frame) - offsetY;
    self.keyBoderHandleHeigt = viewMaxY - (boundHeight-keyBoderRect.size.height);
    
    if (self.keyBoderHandleHeigt <= 0) {
        return;
    }
    
    
    if (self.keyBoderHandleView.superview != nil) {
        
        UIView *superView = self.keyBoderHandleView.superview;
        [UIView animateWithDuration:duration animations:^{
            
            self.keyBoderHandleView.superview.frame = CGRectMake(superView.frame.origin.x, superView.frame.origin.y-self.keyBoderHandleHeigt, superView.bounds.size.width, superView.bounds.size.height);
        }];
        
    }
}


//键盘消失
- (void)keyboardWillHidden:(NSNotification *)note{
    
    // 取出键盘弹出需要花费的时间
    double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        
        UIView *superView = self.keyBoderHandleView.superview;
        self.keyBoderHandleView.superview.frame = CGRectMake(superView.frame.origin.x, superView.frame.origin.y+self.keyBoderHandleHeigt, superView.bounds.size.width, superView.bounds.size.height);
    }];
    
}


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

使用:

[[KeyBoderHandlerTool getSharedKeyBoderTool] handleKeyBoderAction:textField offsetY:0];

注意点:

1:在 确定不需要再监听的情况下可以 调用 工具类中的 销毁方法
2: 发生界面跳转等情况时,请先将键盘隐藏(self.view.endEditing(true))

传送门swift版本:https://www.jianshu.com/p/9b5cd064ea95

相关文章

网友评论

      本文标题:OC 解决键盘遮盖问题,可直接拷贝使用

      本文链接:https://www.haomeiwen.com/subject/gajetctx.html