我只是想记录一下,设置不了文章仅自己可见!!!shit!
最近在做FlutterViewController和iOS混编,项目绝大部分需要竖屏,且不随设备翻转变换方向,flutter插件中的better_player全屏需要屏幕支持横屏,因为flutterView所有的widget都在本身上,所以不能定位到视频界面,只能在viewwillapper中判断。
ps:本来想在pop返回之后做个监听,是视频页返回主页面的时候把屏幕再限定为竖屏,didMoveToParentViewController这些方法什么的,但flutterController就一层没法监听。
1.设置项目设备屏幕旋转方向,这其实不能完全决定旋转方向
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。