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、
勾選需要旋轉的方向。
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自行調整,自己做好橫屏和豎屏判斷。