iOS横屏获取键盘高度为0、键盘侧面弹出问题

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自行调整,自己做好横屏和竖屏判断。

猜你喜欢

转载自blog.csdn.net/zhaotao0617/article/details/78606121
今日推荐