美文网首页
2021-11-24iOS 屏幕旋转

2021-11-24iOS 屏幕旋转

作者: 五蕴盛 | 来源:发表于2021-11-24 11:19 被阅读0次

我只是想记录一下,设置不了文章仅自己可见!!!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。

相关文章

  • 2021-11-24iOS 屏幕旋转

    我只是想记录一下,设置不了文章仅自己可见!!!shit! 最近在做FlutterViewController和iO...

  • iOS 屏幕旋转

    屏幕旋转 认知 期望达到的目的 如何让App支持屏幕旋转 如何让App屏幕旋转 如何保证屏幕旋转后布局不会乱 总结...

  • 屏幕旋转

    屏幕旋转 推荐文档 了解UIWindow——UIWindow实践 iOS屏幕旋转问题总结 IOS:屏幕旋转与变换 ...

  • 屏幕旋转

    UIDevice.current.setValue(UIInterfaceOrientation.landscap...

  • 屏幕旋转

    import "AppDelegate.h" import "ViewController.h" @interfa...

  • 屏幕旋转

    在做工程的时候碰到了屏幕旋转的问题,如今已经解决.为大家分享一下 屏幕旋转机制流程 (1)加速计检测到方向变化,发...

  • 屏幕旋转

    每个视图控制器都控制着自己的旋转方向,如果需要新的旋转权限需要模态出新的视图控制器(如navigation tab...

  • 屏幕旋转

    当activity设置默认属性的时候:竖屏和横屏旋转可以通过监听onConfigurationChanged来判断...

  • 屏幕旋转

    在创建的vc中 //指定能够支持的orientation有哪些 -(UIInterfaceOrientationM...

  • 屏幕旋转

    本文涉及到的转屏是咱们的app的某个页面设置横竖屏的切换 必须先在appdelegate中实现下面的方法-(UII...

网友评论

      本文标题:2021-11-24iOS 屏幕旋转

      本文链接:https://www.haomeiwen.com/subject/ckkntrtx.html