山西坦达清洁设备有限公司

现在的位置: 主页 > 在线留言 > 文章列表

文章正文

Keyboard、In-Call Status Bar的监听及视图位置改变调

作者:山西坦达清洁设备有限公司 来源:www.sxtdqj.com 发布时间:2017-09-08 20:23:09
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

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:湖北网站建设 http://hubei.45qun.com


COPYRIGHT © 2015 山西坦达清洁设备有限公司 ALL RIGHTS RESERVED.
网站地图 技术支持:肥猫科技
精彩专题:网站建设
购买本站友情链接、项目合作请联系客服QQ:2500-38-100