iOS横屏获取键盘高度为0的问题,解决方案

在做视频横竖屏的时候,经常出现键盘的bug,大致分为两种:
1、横屏状态下键盘从home键方向弹出
2、获取键盘高度有时会出现为0的情况
注:可以使用IQKeyboardManager这个框架,导入项目即可。而且不需要计算键盘高度改输入框位置,屏幕内容会自动上移,非常好用。(如果需要横屏操作,也需要按照以下方法来解决)

下边讲解为实现横屏,并不会出现以上键盘bug:

1、在info.plist文件中将 View controller-based status bar appearance 设置为NO
这样设置之后,想改变状态栏颜色和隐藏这样写就可以了

[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
[UIApplication sharedApplication].statusBarHidden = YES;

2、
截图.png

勾选需要旋转的方向。

3、在自己写的NavigationController和TabbarController里边,分别写上以下代码

1、NavigationController里边:
- (BOOL)shouldAutorotate{
   return self.topViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

2、TabbarController里边:
- (BOOL)shouldAutorotate{
   return self.selectedViewController.shouldAutorotate;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait;
}

4、在需要做旋转操作的控制器写上以下代码:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeRight;
}

5、选转屏幕方法代码

SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationLandscapeRight;//旋转的方向
[invocation setArgument:&val atIndex:2];
[invocation invoke];

按照以上步骤,在横屏状态下,键盘弹出不会出现异常,获取键盘高度也会正常
界面UI自行调整,自己做好横屏和竖屏判断。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 春节放假并不意味着工作停顿,各项工作还要继续抓紧时间安排,抓紧时间开展。不慌乱中及时做出下一步的安排和计划。 春节...
    安玲123阅读 436评论 2 1
  • 每一个爱恋藏在秋风里 贯彻你的生命的始终 秋风吹起我的偏头疼 昏昏的下午 在课本中进进出出 我躲在门后 假装没看见...
    koko空空阅读 364评论 2 2
  • 老齐,一个老光棍,平时他就住在一个抗战时期遗留下来的防空洞,老齐已经在那里住了将近20年了,他还记得他刚来时候...
    吕默阅读 846评论 4 6
  • 将数组转化为字符串
    c59ffede9db6阅读 238评论 0 0