2021-11-24iOS 屏幕旋轉(zhuǎn)

我只是想記錄一下,設(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)方向

WeChatb9a134cac5d2349e27ab1b43672a400d.png

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。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容