Keyboard、In-Call Status Bar的监听及视图位置改变调整(实例说明)


写于前:
当有textField的时候,键盘出现时,需要改变其他视图的frame,以及来电时In-Call Status Bar的出现也会影响其他视图的frame,那么通过通知模式,来观察两者的变化,并当其变化时进行一些操作。


代码思路:(视图frame的调整,以创建一个简单toolBar为例来实现)
1、底栏的创建,其中textField设置代理,遵循
2、UITextFieldDelegate协议,设置点击return返回键盘消失
3、通知的注册
4、通知的移除,在viewWillDisappear函数中移除
5、showKeyBoard,hideKeyBoard,statusBarChange分别监测到改变后执行的函数,此例修改bottom的frame,其他操作也可以在此处进行,此例的设置,可以实现statusBar和键盘之间任意情况的组合,不影响bottom的视图位置
(效果图在结尾)


#import "ViewController.h"

#define kwidth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
#define kbottom 49

@interface ViewController ()
{
    UIView *_bottomView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    [self _creatBottomBar];       //创建底栏
    [self _notificationSet];      //通知注册
}

- (void)_notificationSet {
    //监听键盘事件——出现
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showKeyBoard:) name:UIKeyboardWillShowNotification object:nil];
    //监听键盘事件——消失
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hideKeyBoard:) name:UIKeyboardDidHideNotification object:nil];
    //监听状态栏的改变
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(statusBarChange:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
}

- (void)_creatBottomBar {
    //自定制
    _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, kheight-kbottom, kwidth, kbottom)];
    _bottomView.backgroundColor = [UIColor darkGrayColor];

    //输入框
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, kwidth-20, 40)];
    textField.background = [UIImage imageNamed:@"chat_bottom_textfield"];
    //设置代理,要遵循协议UITextFieldDelegate
    textField.delegate = self;
    [_bottomView addSubview:textField];

    [self.view addSubview:_bottomView];
}

当键盘出现时进行的操作

- (void)showKeyBoard:(NSNotification *)notification {
//    NSLog(@"%@",notification.userInfo);     //可以打印看看里面有哪些细节
    NSValue *rectValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [rectValue CGRectValue];
    CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];
    CGFloat keyboardHeight = keyboardFrame.size.height;

    //记录前一次的bottomView的位置
    CGFloat lastBottomY = _bottomView.frame.origin.y;
    //调整frame
    _bottomView.frame = CGRectMake(0, lastBottomY-keyboardHeight, kwidth, kbottom);
}

当键盘隐藏时进行的操作:

- (void)hideKeyBoard:(NSNotification *)notification {
    NSValue *rectValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [rectValue CGRectValue];
    CGRect keyboardFrame = [self.view convertRect:keyboardRect fromView:[[UIApplication sharedApplication] keyWindow]];
    CGFloat keyboardHeight = keyboardFrame.size.height;

    CGFloat lastBottomY = _bottomView.frame.origin.y;
    _bottomView.frame = CGRectMake(0, lastBottomY+keyboardHeight, kwidth, kbottom);
}

In-Call Status Bar的变化:

- (void)statusBarChange:(NSNotification *)notification {
    //获取变化参数
    NSValue *rectValue = [notification.userInfo objectForKey:UIApplicationStatusBarFrameUserInfoKey];
    CGRect statusRect = [rectValue CGRectValue];
    CGRect statusFrame = [self.view convertRect:statusRect fromView:[[UIApplication sharedApplication]keyWindow]];
    CGFloat statusHeight = statusFrame.size.height-20;

    //修改
    CGFloat lastBottomY = _bottomView.frame.origin.y;
    if (statusHeight == 0) {
        _bottomView.frame = CGRectMake(0, lastBottomY+20, kwidth, kbottom);
    }else {
        _bottomView.frame = CGRectMake(0, lastBottomY-statusHeight, kwidth, kbottom);
    }
}

观察者移除

- (void)viewWillDisappear:(BOOL)animated {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
    NSLog(@"解除通知");
}

单击键盘return键,键盘消失

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

效果图:(正常实现所有Keyboard和In-Call Satus Bar所有组合情况)
图1

图2

图3

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值