我只是想記錄一下,設置不了文章僅自己可見!!!shit!
最近在做FlutterViewController和iOS混編,項目絕大部分需要豎屏,且不隨設備翻轉變換方向,flutter插件中的better_player全屏需要屏幕支持橫屏,因為flutterView所有的widget都在本身上,所以不能定位到視頻界面,只能在viewwillapper中判斷。
ps:本來想在pop返回之后做個監聽,是視頻頁返回主頁面的時候把屏幕再限定為豎屏,didMoveToParentViewController這些方法什么的,但flutterController就一層沒法監聽。
1.設置項目設備屏幕旋轉方向,這其實不能完全決定旋轉方向
WeChatb9a134cac5d2349e27ab1b43672a400d.png
2.在AppDelegate中設置,優先級更高的方法,如果這個方法實現,覆蓋info中設置
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
/***是否允許橫屏的標記*/
@property (nonatomic, assign) BOOL allowRotation;
@end
----------------------AppDelegate.m
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;//默認不支持橫屏
}
3.在UIViewController中
當設備發生旋轉時,首先會查看根controller的shouldAutorotate是否允許旋轉,如果允許,再通過
supportedInterfaceOrientations返回的方向 和 系統支持的方向 的交集,判斷當前這個旋轉是否應該發生。
#pragma mark - 屏幕旋轉這三個方法可以在原生正常viewController中起效,對flutterViewController不起作用
///屏幕是否可以選擇,yes
- (BOOL)shouldAutorotate {
return YES;
}
///頁面選擇默認方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
//頁面支持的旋轉方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
---------設置支持旋轉方向+退出界面關閉,可在viewwillapper中調用------
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.isPop)
{
[self endFullScreen];
self.isPop = !self.isPop;
}
}
------------------------------------------------
//進入全屏
-(void)begainFullScreen{
_isPop = YES;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
}
// 退出全屏
-(void)endFullScreen{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//強制歸正:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val =UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}
PS:提示 想監聽側滑返回結果,用下面的方法,但這個在混編項目里不適用,因為flutter只有一個樹
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if (self.navigationController == nil) {
//TODO:你想做的事情
}
NSLog(@"%@: %ld, %@", self, viewCount, self.navigationController);
}
@property(nullable, nonatomic,readonly,strong) UINavigationController *navigationController;//這個屬性
If the view controller or one of its ancestors is a child of a navigation controller, this property contains the owning navigation controller. This property is nil if the view controller is not embedded inside a navigation controller.
如果視圖控制器或其祖先之一是導航控制器的子級,則此屬性包含所屬導航控制器。如果視圖控制器未嵌入導航控制器內,則此屬性為 nil。