美文网首页
iOS review系列之UIViewController

iOS review系列之UIViewController

作者: Jimmy_L_Wang | 来源:发表于2020-04-27 17:07 被阅读0次

    iOS review系列之使用Segues
    iOS review系列之自定义转场动画
    iOS review系列之Presenting a View Controller
    iOS review系列之Creating Custom Presentations

    UIViewController类定义了所有ViewController共有的共享行为。你很少直接创建UIViewController类的实例。相反,你子类化UIViewController并添加管理ViewController的View层次结构所需的方法和属性。

    ViewController的主要职责包括:

    • 更新View的内容,通常是响应底层数据的更改。

    • 响应用户与View的交互。

    • 调整View大小并管理整个界面的布局。

    • 与应用程序中的其他对象(包括其他ViewController)进行协调。

    ViewController与它管理的View紧密绑定,并参与处理View层次结构中的事件。具体来说,ViewController是UIResponder对象,其被插入到ViewController的根视图和根视图的父视图之间的响应者链中,而根视图的父视图通常属于不同的ViewController。如果ViewController的View都不处理事件,ViewController可以选择处理事件或将其传递给根视图的父视图。

    ViewController很少单独使用。相反,你经常使用多个ViewController,每一个都拥有你应用程序用户界面的一部分。例如,一个ViewController可能显示一个列表,而另一个ViewController显示从该列表中选择的项目。通常,一次只有一个ViewController的View是可见的。一个ViewController可以呈现一个不同的ViewController来显示一组新的View,或者它可以作为其他ViewController的内容和动画视图的容器。

    子类化

    每个应用程序至少包含一个UIViewController的自定义子类。更常见的情况是,应用程序包含许多自定义视图控制器。自定义视图控制器定义应用程序的整体行为,包括应用程序的外观以及它如何响应用户交互。下面几节简要概述自定义子类执行的一些任务。有关使用和实现视图控制器的详细信息,请参阅iOS的视图控制器编程指南。

    View Management

    每个ViewController管理一个View层次结构,其根视图存储在这个类的View属性中。根视图主要充当View层次结构其余部分的容器。根视图的大小和位置是由拥有它的对象决定的,它要么是父ViewController,要么是应用程序的窗口。窗口拥有的ViewController是应用程序的根ViewController,它的View大小用于填充窗口。

    ViewController懒加载它们的View。首次访问View属性将加载或创建ViewController的View。有几种方法可以指定ViewController的View:

    • 在应用程序的Storyboard中指定ViewController及其View。Storyboard是指定View的首选方法。使用Storyboard,您可以指定View及其到ViewController的连接。你也可以指定ViewController之间的关系(relationships)和segue, 这使得它更容易看到和修改你app的行为。

      要从Storyboard加载ViewController,调用相应UIStoryboard对象的instantiateViewController(withIdentifier:)方法。storyboard对象创建ViewController并将其返回给你的代码。

    • 使用Nib文件为ViewController指定View。nib文件允许您指定单个ViewController的View,但不允许您定义ViewController之间的segue或关系(relationships)。nib文件也只存储关于ViewController本身的少量信息。

      要使用nib文件初始化ViewController对象,请以编程方式创建ViewController类,并使 用init(nibName:bundle:)方法初始化它。当它的View被请求时,ViewController从nib文件中加载它们。

    • 使用loadView()方法为ViewController指定View。在该方法中,以编程方式创建View层次结构,并将该层次结构的根视图分配给ViewController的View属性。

    所有这些技术都具有相同的最终结果,即创建适当的视图集并通过View属性公开它们。

    ViewController是其View及其创建的任何子View的唯一所有者。它负责创建这些View,并在适当的时候(如ViewController本身释放时)放弃对它们的所有权。如果使用storyboard或nib文件来存储View对象,当ViewController请求时,每个ViewController对象自动获得这些View的副本。但是,如果手动创建view,每个ViewController必须有自己独特的视图集。你不能在ViewController之间共享View。

    ViewController的根视图总是按其分配的空间大小调整大小。对于View层次结构中的其他View,请使用Interface Builder指定自动布局约束,这些约束控制如何在父视图的范围内定位和调整大小。您还可以通过编程方式创建约束,并在适当的时候将它们添加到View中。有关如何创建约束的更多信息,请参见自动布局指南

    处理与视图相关的通知

    当视图的可见性改变时,ViewController会自动调用它自己的方法,这样子类就可以对改变做出响应。使用viewWillAppear(_:)这样的方法来准备视图显示在屏幕上,并使用viewWillDisappear(_:)来保存更改或其他状态信息。使用其他方法进行适当的更改。

    图1显示了视图控制器的视图可能的可见状态以及可能发生的状态转换。并不是所有的
    will回调方法都只与did回调方法配对。您需要确保,如果您在will回调方法中启动一个进程,那么您将在相应的did和相反的will回调方法中结束该进程。

    Figure 1 Valid State Transitions

    处理视图旋转

    从iOS 8开始,所有与旋转相关的方法都被弃用。相反,旋转被视为视图控制器视图大小的变化,因此使用viewWillTransition(to:with:)方法来报告。当界面朝向改变时,UIKit在窗口的根视图控制器上调用这个方法。然后根视图控制器通知它的子视图控制器,在整个视图控制器层次结构中传播消息。

    在iOS 6和iOS 7中,你的应用程序支持在Info.plist文件中定义界面的方向。视图控制器可以覆盖(override)supportedInterfaceOrientations方法来限制被支持的方向列表。通常,系统只在窗口的根视图控制器或某个视图控制器上调用此方法,以填充整个屏幕; 子视图控制器使用父视图控制器提供给它们的窗口部分,不再直接参与关于支持哪些旋转的决策。app的界面方向(orientation mask)和视图控制器的界面方向(orientation mask)是用来确定视图控制器可以旋转到那个方向。

    您可以为视图控制器重写preferredInterfaceOrientationForPresentation方法,该视图控制器将以特定方向全屏显示。

    当一个可见视图控制器发生旋转时,willRotate(to:duration:)willAnimateRotation(to:duration:)didRotate(from:)方法在旋转期间被调用。viewWillLayoutSubviews()方法也会在视图被父视图调整大小和定位之后调用。如果一个视图控制器在发生方向改变时不可见,那么旋转方法就永远不会被调用。但是,当视图变得可见时,将调用viewWillLayoutSubviews()方法。这个方法的实现可以调用statusBarOrientation方法来获取到设备的方向。

    在启动时,应用程序应该始终将其界面设置为纵向(portrait)。在应用程序(_:didFinishLaunchingWithOptions:)方法返回之后,应用程序使用上面描述的视图控制器旋转机制,在显示窗口之前将视图旋转到适当的方向。

    实现容器视图控制器

    一个自定义UIViewController子类也可以作为一个容器视图控制器。容器视图控制器管理它所拥有的其他视图控制器的内容的显示,也称为它的子视图控制器。子视图可以提供原样或结合由容器视图控制器拥有的视图。

    你的容器视图控制器子类应该声明一个公共接口来关联它的子类视图控制器。这些方法的性质取决于您,并且取决于您所创建的容器的语义。您需要决定有多少子类视图控制器可以被显示在你的视图控制器上,当这些子类视图控制器被显示出来, 他们出现在你的视图控制器视图层次中。视图控制器类定义了哪些关系(如果有的话)由子控制器共享。通过为容器建立一个干净的公共接口,您可以确保子类视图控制器在逻辑上使用它的功能,而不需要访问太多关于容器如何实现行为的私有细节。

    在子类视图控制器根视图添加在视图层次之前,容器视图控制器必须关联把自己与子类视图控制器关联起来。这允许iOS正确地将事件路由到子视图控制器和这些控制器管理的视图中。同样,从视图结构中删除一个子视图控制器的根视图, 你应该断开自己与子视图控制器的关联。容器调用基类定义的特定方法。这些方法不打算被容器类的客户端调用;他们只有通过容器的方法实现提供预期的控制行为。

    以下是你可能需要调用的基本方法:

    在创建容器视图控制器时,不需要覆盖任何方法。

    默认情况下,旋转和外观回调会自动转发给子视图控制器。您可以选择覆盖 shouldAutomaticallyForwardRotationMethods()shouldAutomaticallyForwardAppearanceMethods方法来自己控制这种行为。

    内存管理

    内存在iOS中是一种重要的资源,视图控制器为在关键时刻减少内存占用提供了内置支持。UIViewController类通过它的didReceiveMemoryWarning()方法提供了一些对低内存条件下的自动内存处理,释放一些不需要的内存。

    状态保存和恢复

    如果你分配一个值给视图控制器的restorationIdentifier属性,系统可能会要求视图控制器编码本身当应用程序转换到后台时。保留时,视图控制器将保留其视图层次结构中具有恢复标识符(restoration identifiers)的任何视图的状态。视图控制器不会自动保存任何其他状态。如果你正在实现一个自定义容器视图控制器,你必须自己编码任何子视图控制器。您编码的每个子视图控制器必须具有唯一的恢复标识符(restoration identifiers)。

    有关系统如何确定要保存和恢复哪个视图控制器的更多信息,请参见 App Programming Guide for iOS。要查看状态保存和恢复的示例,请参见 Restoring Your App’s State

    Topics

    Creating a View Controller

    init(nibName: String?, bundle: Bundle?)

    Returns a newly initialized view controller with the nib file in the specified bundle.

    使用指定bundle中的nib文件返回新初始化的视图控制器。

    init?(coder: NSCoder)

    Getting the Storyboard and Nib Information

    var storyboard: UIStoryboard?

    The storyboard from which the view controller originated.

    视图控制器起源于的故事板。

    var nibName: String?

    The name of the view controller's nib file, if one was specified.

    视图控制器的nib文件名称,如果被指定的话。

    var nibBundle: Bundle?

    The view controller's nib bundle if it exists.

    视图控制器的nib包如果它存在的话。

    Managing the View

    var view: UIView!

    The view that the controller manages.

    控制器管理的视图。

    var viewIfLoaded: UIView?

    The view controller’s view, or nil if the view is not yet loaded.

    视图控制器的视图,如果视图尚未加载,则为nil

    var isViewLoaded: Bool

    A Boolean value indicating whether the view is currently loaded into memory.

    一个布尔值,指示视图当前是否加载到内存中。

    func loadView()

    Creates the view that the controller manages.

    创建控制器管理的视图。

    func viewDidLoad()

    Called after the controller's view is loaded into memory.

    控制器的视图被加载到内存中之后调用的方法。

    func loadViewIfNeeded()

    Loads the view controller’s view if it has not yet been loaded.

    如果视图控制器的视图尚未加载,则加载该视图控制器的视图。

    var title: String?

    A localized string that represents the view this controller manages.

    表示该控制器管理的视图的本地化字符串。

    var preferredContentSize: CGSize

    The preferred size for the view controller’s view.

    视图控制器视图的首选大小。

    Responding to View-Related Events-响应与视图相关的事件

    func viewWillAppear(Bool)

    Notifies the view controller that its view is about to be added to a view hierarchy.

    通知视图控制器其视图将被添加到视图层次结构中。

    func viewDidAppear(Bool)

    Notifies the view controller that its view was added to a view hierarchy.

    通知视图控制器其视图已添加到视图层次结构中。

    func viewWillDisappear(Bool)

    Notifies the view controller that its view is about to be removed from a view hierarchy.

    通知视图控制器其视图将从视图层次结构中移除。

    func viewDidDisappear(Bool)

    Notifies the view controller that its view was removed from a view hierarchy.

    通知视图控制器其视图已从视图层次结构中删除。

    var isBeingDismissed: Bool

    A Boolean value indicating whether the view controller is being dismissed.

    一个布尔值,指示视图控制器是否正在被取消。

    var isBeingPresented: Bool

    A Boolean value indicating whether the view controller is being presented.

    一个布尔值,指示是否显示视图控制器。

    var isMovingFromParent: Bool

    A Boolean value indicating whether the view controller is being removed from a parent view controller.

    一个布尔值,指示视图控制器是否从父视图控制器中移除。

    var isMovingToParent: Bool

    A Boolean value indicating whether the view controller is being moved to a parent view controller.

    一个布尔值,指示是否将视图控制器移动到父视图控制器。

    Extending the View's Safe Area-扩展视图的安全区域

    Positioning Content Relative to the Safe Area

    Position views so that they are not obstructed by other content.

    定位视图,使它们不被其他内容所阻碍。

    var additionalSafeAreaInsets: UIEdgeInsets

    Custom insets that you specify to modify the view controller's safe area.

    用于修改视图控制器安全区的自定义insets。

    func viewSafeAreaInsetsDidChange()

    Called to notify the view controller that the safe area insets of its root view changed.

    调用此函数以通知视图控制器其根视图的安全区域已更改

    Managing the View's Margins-管理视图的边距

    Positioning Content Within Layout Margins

    Position views so that they are not crowded by other content.

    定位视图,使其不被其他内容挤塞。

    var viewRespectsSystemMinimumLayoutMargins: Bool

    A Boolean value indicating whether the view controller's view uses the system-defined minimum layout margins.

    一个布尔值,指示视图控制器的视图是否使用系统定义的最小布局边距。

    var systemMinimumLayoutMargins: NSDirectionalEdgeInsets

    The minimum layout margins for the view controller's root view.

    视图控制器根视图的最小布局边距。

    func viewLayoutMarginsDidChange()

    Called to notify the view controller that the layout margins of its root view changed.

    调用此函数以通知视图控制器其根视图的布局页边距已更改。

    Configuring the View’s Layout Behavior-配置视图的布局行为

    var edgesForExtendedLayout: UIRectEdge

    The edges that you extend for your view controller.

    你为视图控制器扩展的边。

    struct UIRectEdge

    Constants that specify the edges of a rectangle.

    指定矩形边缘的常数。

    var extendedLayoutIncludesOpaqueBars: Bool

    A Boolean value indicating whether or not the extended layout includes opaque bars.

    一个布尔值,指示扩展布局是否包含不透明条。

    func viewWillLayoutSubviews()

    Called to notify the view controller that its view is about to layout its subviews.

    调用此函数以通知视图控制器其视图即将布局其子视图。

    func viewDidLayoutSubviews()

    Called to notify the view controller that its view has just laid out its subviews.

    调用它来通知视图控制器它的视图刚刚布局出了它的子视图。

    func updateViewConstraints()

    Called when the view controller's view needs to update its constraints.

    视图控制器的视图需要更新其约束调用此方法。

    Configuring the View Rotation Settings-配置视图旋转设置

    var shouldAutorotate: Bool

    Returns a Boolean value indicating whether the view controller's contents should auto rotate.

    返回一个布尔值,指示是否视图控制器的内容应该自动旋转。

    var supportedInterfaceOrientations: UIInterfaceOrientationMask

    Returns all of the interface orientations that the view controller supports.

    返回视图控制器支持的所有界面方向。

    var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation

    Returns the interface orientation to use when presenting the view controller.

    返回显示视图控制器时使用的界面方向。

    class func attemptRotationToDeviceOrientation()

    Attempts to rotate all windows to the orientation of the device.

    尝试将所有窗口旋转到设备的方向。

    Performing Segues

    func shouldPerformSegue(withIdentifier: String, sender: Any?) -> Bool

    Determines whether the segue with the specified identifier should be performed.

    确定是否应执行具有指定标识符的segue。

    func prepare(for: UIStoryboardSegue, sender: Any?)

    Notifies the view controller that a segue is about to be performed.

    通知视图控制器一个segue将要被执行。

    func performSegue(withIdentifier: String, sender: Any?)

    Initiates the segue with the specified identifier from the current view controller's storyboard file.

    发起segue与指定的标识符从当前视图控制器的故事板文件。

    func allowedChildrenForUnwinding(from: UIStoryboardUnwindSegueSource) -> [UIViewController\]

    Returns an array of child view controllers to search for an unwind segue destination.
    返回一组子视图控制器,以搜索unwind segue的目的地。

    func childContaining(UIStoryboardUnwindSegueSource) -> UIViewController?

    Returns the child view controller that contains the source of the unwind segue.
    返回包含unwind segue源的子视图控制器。

    func canPerformUnwindSegueAction(Selector, from: UIViewController, sender: Any?) -> Bool

    Called on a view controller to determine whether it responds to an unwind action.

    在视图控制器上调用,以确定它是否响应unwind操作。

    func unwind(for: UIStoryboardSegue, towards: UIViewController)

    Called when an unwind segue transitions to a new view controller.

    在unwind segue过渡到新视图控制器时调用。

    Presenting a View Controller-呈现视图控制器

    func show(UIViewController, sender: Any?)

    Presents a view controller in a primary context.

    在主上下文中显示视图控制器。

    func showDetailViewController(UIViewController, sender: Any?)

    Presents a view controller in a secondary (or detail) context.

    提供了一个视图控制器在二级(或细节)上下文。

    func present(UIViewController, animated: Bool, completion: (() -> Void)?)

    Presents a view controller modally.

    模态地呈现一个视图控制器。

    func dismiss(animated: Bool, completion: (() -> Void)?)

    Dismisses the view controller that was presented modally by the view controller.

    解散由视图控制器模态呈现的视图控制器。

    var modalPresentationStyle: UIModalPresentationStyle

    The presentation style for modally presented view controllers.

    模态呈现的视图控制器的表示样式。

    enum UIModalPresentationStyle

    Modal presentation styles available when presenting view controllers.

    显示视图控制器时可用的模式表示样式。

    var modalTransitionStyle: UIModalTransitionStyle

    The transition style to use when presenting the view controller.

    显示视图控制器时要使用的转换样式。

    enum UIModalTransitionStyle

    Transition styles available when presenting view controllers.

    显示视图控制器时可用的转换样式。

    var isModalInPresentation: Bool

    A Boolean value indicating whether the view controller enforces a modal behavior.

    一个布尔值,指示视图控制器是否强制模态行为。

    var definesPresentationContext: Bool

    A Boolean value that indicates whether this view controller's view is covered when the view controller or one of its descendants presents a view controller.

    一个布尔值,指示当视图控制器或其后代之一呈现视图控制器时是否覆盖该视图控制器的视图。

    var providesPresentationContextTransitionStyle: Bool

    A Boolean value that indicates whether the view controller specifies the transition style for view controllers it presents.

    一个布尔值,指示视图控制器是否为其呈现的视图控制器指定转换样式。

    var disablesAutomaticKeyboardDismissal: Bool

    Returns a Boolean indicating whether the current input view is dismissed automatically when changing controls.

    返回一个布尔值,指示在更改控件时是否自动放弃当前输入视图。

    class let showDetailTargetDidChangeNotification: NSNotification.Name

    Posted when a split view controller is expanded or collapsed.

    当分屏视图控制器展开或折叠时发布的通知。

    Adding a Custom Transition or Presentation-添加自定义转场或展示

    var transitioningDelegate: UIViewControllerTransitioningDelegate?

    The delegate object that provides transition animator, interactive controller, and custom presentation controller objects.

    提供转换动画器、交互式控制器和自定义展示控制器对象的委托对象。

    var transitionCoordinator: UIViewControllerTransitionCoordinator?

    Returns the active transition coordinator object.

    返回活动状态的转场协调器对象。

    func targetViewController(forAction: Selector, sender: Any?) -> UIViewController?

    Returns the view controller that responds to the action.

    返回响应操作的视图控制器。

    var presentationController: UIPresentationController?

    The presentation controller that’s managing the current view controller.

    管理当前视图控制器的展示控制器。

    var popoverPresentationController: UIPopoverPresentationController?

    The nearest popover presentation controller that is managing the current view controller.

    管理当前视图控制器的最近的弹出窗口展示控制器。

    var restoresFocusAfterTransition: Bool

    A Boolean value that indicates whether an item that previously was focused should again become focused when the item's view controller becomes visible and focusable.

    一个布尔值,指示当项目的视图控制器变得可见和可聚焦时,先前被聚焦的项目是否应该再次被聚焦。

    Adapting to Environment Changes-适应环境变化

    func collapseSecondaryViewController(UIViewController, for: UISplitViewController)

    Called when a split view controller transitions to a compact-width size class.

    当拆分视图控制器转换为紧凑宽度大小类时调用。

    func separateSecondaryViewController(for: UISplitViewController) -> UIViewController?

    Called when a split view controller transitions to a regular-width size class.

    当拆分视图控制器转换为常规宽度大小的类时调用。

    Adjusting the Interface Style

    var overrideUserInterfaceStyle: UIUserInterfaceStyle

    The user interface style adopted by the view controller and all of its children.

    视图控制器及其所有子控制器所采用的用户界面样式。

    var preferredUserInterfaceStyle: UIUserInterfaceStyle

    The preferred interface style for this view controller.

    此视图控制器的首选接口样式。

    var childViewControllerForUserInterfaceStyle: UIViewController?

    The child view controller that supports the preferred user interface style.

    支持首选用户界面样式的子视图控制器。

    func setNeedsUserInterfaceAppearanceUpdate()

    Notifies the view controller that a change occurred that might affect the preferred interface style.

    通知视图控制器发生了可能影响首选界面样式的更改。

    enum UIUserInterfaceStyle

    Constants indicating the interface style for the app.

    表示应用程序的界面样式的常量。

    Managing Child View Controllers in a Custom Container-在自定义容器中管理子视图控制器

    var children: [UIViewController]

    An array of view controllers that are children of the current view controller.

    当前视图控制器的子视图控制器的数组。

    func addChild(UIViewController)

    Adds the specified view controller as a child of the current view controller.

    将指定的视图控制器添加为当前视图控制器的一个子控制器。

    func removeFromParent()

    Removes the view controller from its parent.

    从其父控制器中移除视图控制器。

    func transition(from: UIViewController, to: UIViewController, duration: TimeInterval, options: UIView.AnimationOptions, animations: (() -> Void)?, completion:((Bool) -> Void)?)

    Transitions between two of the view controller's child view controllers.

    在控制器的两个子视图控制器中进行转场

    var shouldAutomaticallyForwardAppearanceMethods: Bool

    Returns a Boolean value indicating whether appearance methods are forwarded to child view controllers.

    返回一个布尔值,该值指示是否将外观方法转发给子视图控制器。

    func beginAppearanceTransition(Bool, animated: Bool)

    Tells a child controller its appearance is about to change.

    告诉子控制器它的外观即将改变。

    func endAppearanceTransition()

    Tells a child controller its appearance has changed.

    告诉子控制器它的外观已经改变。

    func setOverrideTraitCollection(UITraitCollection?, forChild: UIViewController)

    Changes the traits assigned to the specified child view controller.

    更改分配给指定子视图控制器的特征。

    func overrideTraitCollection(forChild: UIViewController) -> UITraitCollection?

    Retrieves the trait collection for a child view controller.

    检索子视图控制器的特征集合。

    class let hierarchyInconsistencyException: NSExceptionName

    Raised if the view controller hierarchy is inconsistent with the view hierarchy.

    如果视图控制器层次结构与视图层次结构不一致,则引发。

    Responding to Containment Events

    func willMove(toParent: UIViewController?)

    Called just before the view controller is added or removed from a container view controller.

    在视图控制器添加或从容器视图控制器中删除之前调用。

    func didMove(toParent: UIViewController?)

    Called after the view controller is added or removed from a container view controller.

    在容器视图控制器中添加或删除视图控制器后调用。

    Getting Other Related View Controllers

    var presentingViewController: UIViewController?

    The view controller that presented this view controller.

    呈现这个视图控制器的视图控制器。

    var presentedViewController: UIViewController?

    The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy.

    由这个视图控制器或它在视图控制器层次结构中的一个祖先所呈现的视图控制器。

    var parent: UIViewController?

    The parent view controller of the recipient.

    接收者的父视图控制器。

    var splitViewController: UISplitViewController?

    The nearest ancestor in the view controller hierarchy that is a split view controller.

    视图控制器层次结构中最近的祖先,它是一个分屏视图控制器。

    var navigationController: UINavigationController?

    The nearest ancestor in the view controller hierarchy that is a navigation controller.

    视图控制器层次结构中最近的祖先,即导航控制器。

    var tabBarController: UITabBarController?

    The nearest ancestor in the view controller hierarchy that is a tab bar controller.

    视图控制器层次结构中最近的祖先,即选项卡栏控制器。

    Configuring a Navigation Interface-配置导航界面

    var navigationItem: UINavigationItem

    The navigation item used to represent the view controller in a parent's navigation bar.

    导航项用于表示在父视图控制器的导航栏。

    var hidesBottomBarWhenPushed: Bool

    A Boolean value indicating whether the toolbar at the bottom of the screen is hidden when the view controller is pushed on to a navigation controller.

    一个布尔值,指示在将视图控制器推送到导航控制器时,屏幕底部的工具栏是否隐藏。

    func setToolbarItems([UIBarButtonItem]?, animated: Bool)

    Sets the toolbar items to be displayed along with the view controller.

    设置要与视图控制器一起显示的工具栏项。

    var toolbarItems: [UIBarButtonItem]?

    The toolbar items associated with the view controller.

    与视图控制器关联的工具栏项。

    Configuring Tab Bar Content - 配置选项卡栏内容

    var tabBarItem: UITabBarItem!

    The tab bar item that represents the view controller when added to a tab bar controller.

    当添加到选项卡栏控制器时,代表视图控制器的选项卡栏项。

    var tabBarObservedScrollView: UIScrollView?

    The full-screen scroll view to synchronize with a scrolling tab bar.

    全屏滚动视图与滚动标签栏同步。

    Supporting App Extensions - 支持应用程序扩展

    var extensionContext: NSExtensionContext?

    Returns the extension context of the view controller.

    返回视图控制器的扩展上下文。

    Coordinating with System Gestures - 与系统手势协调

    var preferredScreenEdgesDeferringSystemGestures: UIRectEdge

    The screen edges for which you want your gestures to take precedence over the system gestures

    您希望您的手势优先于系统手势的屏幕边缘

    var childForScreenEdgesDeferringSystemGestures: UIViewController?

    Returns the child view controller that should be queried to see if its gestures should take precedence.

    返回应查询其手势是否优先的子视图控制器。

    func setNeedsUpdateOfScreenEdgesDeferringSystemGestures()

    Call this method when you change the screen edges that you use for deferring system gestures.

    当您更改用于延迟系统动作的屏幕边缘时,请调用此方法。

    var prefersHomeIndicatorAutoHidden: Bool

    Returns a Boolean indicating whether the system is allowed to hide the visual indicator for returning to the Home screen.

    返回一个布尔值,指示是否允许系统隐藏返回主屏幕的可视指示器。

    var childForHomeIndicatorAutoHidden: UIViewController?

    Returns the child view controller that is consulted about its preference for displaying a visual indicator for returning to the Home screen.

    返回子视图控制器,该控制器用于显示返回主屏幕的可视指示器的首选项。

    func setNeedsUpdateOfHomeIndicatorAutoHidden()

    Notifies UIKit that your view controller updated its preference regarding the visual indicator for returning to the Home screen.

    通知UIKit你的视图控制器更新了它关于返回主屏幕的可视指示器的首选项。

    Managing the Status Bar

    var childForStatusBarHidden: UIViewController?

    Called when the system needs the view controller to use for determining status bar hidden/unhidden state.

    当系统需要视图控制器用于确定状态栏隐藏/未隐藏状态时调用。

    var childForStatusBarStyle: UIViewController?

    Called when the system needs the view controller to use for determining status bar style.

    当系统需要视图控制器用于确定状态栏样式时调用。

    var preferredStatusBarStyle: UIStatusBarStyle

    The preferred status bar style for the view controller.

    视图控制器的首选状态栏样式。

    var prefersStatusBarHidden: Bool

    Specifies whether the view controller prefers the status bar to be hidden or shown.

    指定视图控制器希望隐藏还是显示状态栏。

    var modalPresentationCapturesStatusBarAppearance: Bool

    Specifies whether a view controller, presented non-fullscreen, takes over control of status bar appearance from the presenting view controller.

    指定显示为非全屏的视图控制器是否从显示视图控制器接管状态栏外观的控制。

    var preferredStatusBarUpdateAnimation: UIStatusBarAnimation

    Specifies the animation style to use for hiding and showing the status bar for the view controller.

    指定用于隐藏和显示视图控制器状态栏的动画样式。

    func setNeedsStatusBarAppearanceUpdate()

    Indicates to the system that the view controller status bar attributes have changed.

    指示系统视图控制器状态栏属性已更改。

    Accessing the Available Key Commands

    var performsActionsWhilePresentingModally: Bool

    A Boolean value indicating whether the view controller performs menu-related actions.

    一个布尔值,指示视图控制器是否执行与菜单相关的操作。

    func addKeyCommand(UIKeyCommand)

    Associates the specified keyboard shortcut with the view controller.

    将指定的键盘快捷键与视图控制器关联。

    func removeKeyCommand(UIKeyCommand)

    Removes the key command from the view controller.

    从视图控制器中删除键命令。

    Adding Editing Behaviors to Your View Controller - 向视图控制器添加编辑行为

    var isEditing: Bool

    A Boolean value indicating whether the view controller currently allows the user to edit the view contents.

    一个布尔值,指示视图控制器当前是否允许用户编辑视图内容。

    func setEditing(Bool, animated: Bool)

    Sets whether the view controller shows an editable view.

    设置视图控制器是否显示可编辑视图。

    var editButtonItem: UIBarButtonItem

    Returns a bar button item that toggles its title and associated state between Edit and Done.

    返回一个bar按钮项,用于在“编辑”和“完成”之间切换其标题和关联状态。

    Handling Memory Warnings

    func didReceiveMemoryWarning()

    Sent to the view controller when the app receives a memory warning.

    当应用程序收到内存警告时发送到视图控制器。

    Managing State Restoration - 管理状态恢复

    Restoring Your App’s State

    Preserve and restore information related to the user’s current activities.

    保存和恢复与用户当前活动相关的信息。

    var restorationIdentifier: String?

    The identifier that determines whether the view controller supports state restoration.

    确定视图控制器是否支持状态恢复的标识符。

    var restorationClass: UIViewControllerRestoration.Type?

    The class responsible for recreating this view controller when restoring the app's state.

    类负责恢复时重新创建这个视图控制器应用的应用状态。

    func encodeRestorableState(with: NSCoder)

    Encodes state-related information for the view controller.

    为视图控制器编码状态相关信息。

    func decodeRestorableState(with: NSCoder)

    Decodes and restores state-related information for the view controller.

    解码并恢复视图控制器的状态相关信息。

    func applicationFinishedRestoringState()

    Called on restored view controllers after other object decoding is complete.

    在其他对象解码完成后,在恢复的视图控制器上调用。

    相关文章

      网友评论

          本文标题:iOS review系列之UIViewController

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