KeyboardNotificationViewController

GitHub地址:https://github.com/liuyongfa/KeyboardNotificationViewController.git

继承KeyboardNotificationViewController,并且重写函数

- (UIView *)bottomViewVisible {
    return self.loginButton;
}

返回最底部不想被键盘覆盖的控件。就可以实现view跟随键盘移动。同时支持点击空白处收起键盘。

LYFKeyboardNotificationViewController.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface LYFKeyboardNotificationViewController : UIViewController
/**
 继承GMCKeyboardNotificationViewController的子类提供给父类
 
 @return  最底部希望不被键盘覆盖的控件
 */
- (nullable UIView *)bottomViewVisible;
@end
NS_ASSUME_NONNULL_END

LYFKeyboardNotificationViewController.m

#import "LYFKeyboardNotificationViewController.h"

static CGFloat VerticalOffset = 8.0;

@interface LYFKeyboardNotificationViewController ()

@end

@implementation LYFKeyboardNotificationViewController

//storyboard
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addKeyboardNotification];
    }
    return self;
}
//非storyboard
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self  = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self addKeyboardNotification];
    }
    return  self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createGestureRecognizer];
}

- (UIView *)bottomViewVisible {
    return nil;
}

#pragma mark - KeyboardNotification
- (void)addKeyboardNotification {
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShowAnimated:(CGRect)keyboardFrame {
    UIView *v = [self bottomViewVisible];
    if (!v) {
        return;
    }
    CGRect buttonRect = [v.superview convertRect:v.frame toView:self.view];
    CGFloat height = self.view.frame.size.height - CGRectGetMaxY(buttonRect);
    CGFloat needH = MIN(0, height - keyboardFrame.size.height - VerticalOffset);
    self.view.transform = CGAffineTransformMakeTranslation(0, needH);
}

- (void)keyboardWillHideAnimated:(CGRect)keyboardFrame {
    self.view.transform = CGAffineTransformMakeTranslation(0, 0);
}

- (void)keyboardWillShowNotification:(NSNotification*)notification {
    [self handleNotification:notification willShow:YES];
}

- (void)keyboardWillHideNotification:(NSNotification*)notification {
    [self handleNotification:notification willShow:NO];
}

- (void)handleNotification:(NSNotification *)notification willShow:(BOOL)show {
    NSDictionary *userInfo = notification.userInfo;
    CGRect frame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    int curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
    __weak LYFKeyboardNotificationViewController *weakSelf = self;
    [UIView animateWithDuration:duration delay:0 options:curve animations:^{
        show ? [weakSelf keyboardWillShowAnimated:frame] : [weakSelf keyboardWillHideAnimated:frame];
    } completion:^(BOOL finished) {
    }];
}

#pragma mark - TapGesture
-(void)createGestureRecognizer {
    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnView)];
    [self.view addGestureRecognizer:tapGR];
}

-(void)tapOnView {
    [self.view endEditing:YES];
}

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

@end

 

转载于:https://www.cnblogs.com/liuyongfa/p/10001723.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值