在iOS 开发中,模态视图控制器可用于在当前视图控制器之上显示一个新视图控制器。模态视图控制器是一种临时的视图控制器,旨在提供特定的功能或信息,无需完全替换当前视图控制器。
要以编程方式显示模态视图控制器,可以使用
PresentModalViewController()
方法。该方法具有以下语法:
func presentModalViewController(_ viewControllerToPresent: UIViewController, aniMated: Bool)
viewControllerToPresent
: 要显示的模态视图控制器。
animated
: 布尔值,指示过渡是否应动画。
let modalViewController = MyModalViewController()presentModalViewController(modalViewController, animated: true)
modalPresentationStyle
属性指定模态视图控制器的样式。以下是一些可用的样式:
UIModalPresentationStyle.fullScreen
:模态视图控制器覆盖整个屏幕。
UIModalPresentationStyle.pageSheet
:模态视图控制器以页面大小的表的形式呈现。
UIModalPresentationStyle.Formsheet
:模态视图控制器以表的形式呈现,并具有一个标题栏和一个底部栏。
UIModalPresentationStyle.currentContext
:模态视图控制器以当前上下文的样式呈现。
UIModalPresentationStyle.overCurrentContext
:模态视图控制器覆盖当前上下文。
UIModalPresentationStyle.overFullScreen
:模态视图控制器覆盖整个屏幕,并在当前视图控制器之上。示例:
modalViewController.modalPresentationStyle = .pageSheet
dismissModalViewControllerAnimated()
方法。在模态视图控制器上点击关闭按钮(如果存在)。在模态视图控制器外部轻按(如果
modalPresentationStyle
设置为
UIModalPresentationStyle.overCurrentContext
或
UIModalPresentationStyle.overFullScreen
)。示例:
dismissModalViewControllerAnimated(true)
PresentModalViewController()
方法以编程方式显示模态视图控制器是一种在 iOS 应用中显示临时视图控制器并提供特定功能或信息的有效方法。了解模态视图控制器样式和关闭模态视图控制器的不同方法对于有效使用它们至关重要。
Netflix资讯
本文地址:https://www.badfl.com/article/2d71ae2657a057bc0c6c.html