iOS13适配—— presentViewController模态弹出默认样式改变

为什么在 iOS13中,presentViewController新的视图控制器时不是全屏的?
在 iOS13中,presentViewController新的视图控制器时,是以分页模式展示的,而不是以往的全屏模式。
原因是因为苹果将 UIViewControllermodalPresentationStyle 属性的默认值改成了新加的一个枚举值 UIModalPresentationAutomatic,如下示例代码。

typedef NS_ENUM(NSInteger, UIModalPresentationStyle) {
    UIModalPresentationFullScreen = 0,
    UIModalPresentationPageSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationFormSheet API_AVAILABLE(ios(3.2)) API_UNAVAILABLE(tvos),
    UIModalPresentationCurrentContext API_AVAILABLE(ios(3.2)),
    UIModalPresentationCustom API_AVAILABLE(ios(7.0)),
    UIModalPresentationOverFullScreen API_AVAILABLE(ios(8.0)),
    UIModalPresentationOverCurrentContext API_AVAILABLE(ios(8.0)),
    UIModalPresentationPopover API_AVAILABLE(ios(8.0)) API_UNAVAILABLE(tvos),
    UIModalPresentationBlurOverFullScreen API_AVAILABLE(tvos(11.0)) API_UNAVAILABLE(ios) API_UNAVAILABLE(watchos),
    UIModalPresentationNone API_AVAILABLE(ios(7.0)) = -1,
    UIModalPresentationAutomatic API_AVAILABLE(ios(13.0)) = -2,
};

因此,对于多数 UIViewController,此值会映射成 UIModalPresentationPageSheet

那要怎么改成全屏模式呢?
只要设置被presentViewController出来的新的UIViewController的属性modalPresentationStyle值为UIModalPresentationFullScreen就可以了。
示例代码如下:

NextViewController *nextVC = [NextViewController new];
nextVC.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:nextVC animated:YES completion:NULL];

上面的写法是没有UINavigationControllerUIViewController,如果带有UINavigationControllerUIViewControllerpresentViewController要怎么写?
同样是设置UINavigationController的属性modalPresentationStyle值为UIModalPresentationFullScreen就可以了。
示例代码如下:

NextViewController *nextVC = [NextViewController new];
UINavigationController *nextNav = [[UINavigationController alloc] initWithRootViewController:nextVC];
nextNav.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:nextNav animated:YES completion:NULL];

除了这种写法外,还有其他写法吗?
有的,如果是没有UINavigationController的话,可以在UIViewController.m文件里实现,且有两种写法,写法如下:

//
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
 	self.modalPresentationStyle = UIModalPresentationFullScreen;
}
// 重写 get 方法
- (UIModalPresentationStyle)modalPresentationStyle
{
    return UIModalPresentationFullScreen;
}

如果是有UINavigationController的话呢,又应该怎么写?
如果是有UINavigationController的话,也可以在UIViewController.m文件里实现,写法如下:

//
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
 	self.navigationController.modalPresentationStyle = UIModalPresentationFullScreen;
}

以上纯属个人认知,不足之外,还请大家留言共同学习讨论。

发布了804 篇原创文章 · 获赞 135 · 访问量 142万+

猜你喜欢

转载自blog.csdn.net/potato512/article/details/104871676