Menu

  • 博客
  • 关于唯我&博客
  • 唯我DIY
  • 讨论区

Copyright © VIIIO.COM | Theme by Theme in Progress | 基于 WordPress

千里之行,始于足下唯我 - 梦想从此起航

iOS 老生常談的鍵盤遮擋問題

2015年12月26日iOS Standard
Views: 1,275

 一、如果在tableview內,可以採用tableview偏移來解決:

– (void)textFieldDidBeginEditing:(UITextField *)textField{

    if ([textField isEqual:XXXX]) {

        CGPoint origin = textField.frame.origin;

        CGPoint point = [textField.superview convertPoint:origin toView:self.tableView];

        float navBarHeight = self.navigationController.navigationBar.frame.size.height;

        CGPoint offset = self.tableView.contentOffset;

        // change offsetY

        offset.y = (point.y – 216 – navBarHeight-40);//216固定鍵盤高度,雖然不精准但勉強可以用

        [self.tableView setContentOffset:offset animated:YES];

    }

}

二、比較通用的遮擋處理,利用內置UIKeyboard通知

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

    [center addObserver:self selector:@selector(keyboardWillShow:)  name:UIKeyboardWillShowNotification  object:nil];//鍵盤彈出

    [center addObserver:self selector:@selector(keyboardDidHide)  name:UIKeyboardWillHideNotification object:nil]; //鍵盤收起

– (void)keyboardWillShow:(NSNotification *)aNotification{

    NSDictionary *userInfo = [aNotification userInfo];

    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];

    CGFloat _keyboardHeight = ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) ? keyboardRect.size.height : keyboardRect.size.width;//獲取到鍵盤高度

    //接下來想怎麼做都可以,改變ViewOffset、Frame等等   然後在keyboardHide事件中還原就可以
//示例:

    [UIView animateWithDuration:duration animations:^{

        self.view.frame = CGRectMake(0.0f, -1 * _keyboardHeight, self.view.frame.size.width, self.view.frame.size.height);

    }];

} 

 

发表评论或回复 取消回复

邮箱地址不会被公开。

5 + 4 =

近期文章

  • OC UIWindow setRootViewController切换界面引发的内存问题
  • iOS证书、AppId、PP文件之间的关系
  • SVN服务器搭建、备份及多服务器同步方案(Windows)
  • [转]iOS多线程-各种线程锁的简单介绍
  • Mac 下Apache2 配置多虚拟主机

近期评论

  • NARYTHY288954NEYRTHYT发表在《ASP.NET整合Discuz PHP站 并实现用户同步》
  • nym402059flebno发表在《ASP.NET整合Discuz PHP站 并实现用户同步》
  • nem2182758krya发表在《ASP.NET整合Discuz PHP站 并实现用户同步》
  • aresgrb.se发表在《ASP.NET整合Discuz PHP站 并实现用户同步》
  • Vincenturbam发表在《ASP.NET整合Discuz PHP站 并实现用户同步》

分类目录

  • ASP.NET (15)
  • Git (2)
  • HTML (1)
  • iOS (31)
  • Javascript (7)
  • Oracle (8)
  • SQL (3)
  • SQLSERVER (2)
  • SVN (1)
  • 一行代码系列 (5)
  • 微信小程序 (1)
  • 正则表达式 (2)
  • 网站建设 (5)

文章归档

  • 2018年12月 (1)
  • 2018年4月 (1)
  • 2017年12月 (2)
  • 2017年7月 (3)
  • 2017年6月 (1)
  • 2017年4月 (1)
  • 2017年1月 (1)
  • 2016年12月 (3)
  • 2016年10月 (1)
  • 2016年7月 (1)
  • 2016年6月 (1)
  • 2016年5月 (3)
  • 2016年4月 (5)
  • 2016年3月 (4)
  • 2016年2月 (2)
  • 2016年1月 (3)
  • 2015年12月 (11)
  • 2015年11月 (7)
  • 2015年10月 (3)
  • 2015年9月 (1)
  • 2015年8月 (1)
  • 2015年7月 (1)
  • 2015年6月 (1)
  • 2015年5月 (1)
  • 2015年4月 (1)
  • 2014年7月 (1)
  • 2014年6月 (1)
  • 2014年5月 (2)
  • 2014年4月 (2)
  • 2014年3月 (2)
  • 2014年2月 (2)
2025年5月
一 二 三 四 五 六 日
« 12月    
 1234
567891011
12131415161718
19202122232425
262728293031