我只是想記錄一下,設(shè)置不了文章僅自己可見(jiàn)!??!shit!
最近在做FlutterViewController和iOS混編,項(xiàng)目絕大部分需要豎屏,且不隨設(shè)備翻轉(zhuǎn)變換方向,flutter插件中的better_player全屏需要屏幕支持橫屏,因?yàn)閒lutterView所有的widget都在本身上,所以不能定位到視頻界面,只能在viewwillapper中判斷。
ps:本來(lái)想在pop返回之后做個(gè)監(jiān)聽(tīng),是視頻頁(yè)返回主頁(yè)面的時(shí)候把屏幕再限定為豎屏,didMoveToParentViewController這些方法什么的,但flutterController就一層沒(méi)法監(jiān)聽(tīng)。
1.設(shè)置項(xiàng)目設(shè)備屏幕旋轉(zhuǎn)方向,這其實(shí)不能完全決定旋轉(zhuǎn)方向

2.在AppDelegate中設(shè)置,優(yōu)先級(jí)更高的方法,如果這個(gè)方法實(shí)現(xiàn),覆蓋info中設(shè)置
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
/***是否允許橫屏的標(biāo)記*/
@property (nonatomic, assign) BOOL allowRotation;
@end
----------------------AppDelegate.m
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.allowRotation) {
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;//默認(rèn)不支持橫屏
}
3.在UIViewController中
當(dāng)設(shè)備發(fā)生旋轉(zhuǎn)時(shí),首先會(huì)查看根controller的shouldAutorotate是否允許旋轉(zhuǎn),如果允許,再通過(guò)
supportedInterfaceOrientations返回的方向 和 系統(tǒng)支持的方向 的交集,判斷當(dāng)前這個(gè)旋轉(zhuǎn)是否應(yīng)該發(fā)生。
#pragma mark - 屏幕旋轉(zhuǎn)這三個(gè)方法可以在原生正常viewController中起效,對(duì)flutterViewController不起作用
///屏幕是否可以選擇,yes
- (BOOL)shouldAutorotate {
return YES;
}
///頁(yè)面選擇默認(rèn)方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeRight;
}
//頁(yè)面支持的旋轉(zhuǎn)方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
---------設(shè)置支持旋轉(zhuǎn)方向+退出界面關(guān)閉,可在viewwillapper中調(diào)用------
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (self.isPop)
{
[self endFullScreen];
self.isPop = !self.isPop;
}
}
------------------------------------------------
//進(jìn)入全屏
-(void)begainFullScreen{
_isPop = YES;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = YES;
}
// 退出全屏
-(void)endFullScreen{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.allowRotation = NO;
//強(qiáng)制歸正:
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:提示 想監(jiān)聽(tīng)側(cè)滑返回結(jié)果,用下面的方法,但這個(gè)在混編項(xiàng)目里不適用,因?yàn)閒lutter只有一個(gè)樹(shù)
- (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;//這個(gè)屬性
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.
如果視圖控制器或其祖先之一是導(dǎo)航控制器的子級(jí),則此屬性包含所屬導(dǎo)航控制器。如果視圖控制器未嵌入導(dǎo)航控制器內(nèi),則此屬性為 nil。