为什么在 iOS13中,presentViewController新的视图控制器时不是全屏的?
在 iOS13中,presentViewController
新的视图控制器时,是以分页模式展示的,而不是以往的全屏模式。
原因是因为苹果将 UIViewController
的 modalPresentationStyle
属性的默认值改成了新加的一个枚举值 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];
上面的写法是没有UINavigationController的UIViewController,如果带有UINavigationController的UIViewController被presentViewController要怎么写?
同样是设置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;
}
以上纯属个人认知,不足之外,还请大家留言共同学习讨论。