Resizing the View Controller’s Views
A view controller owns its own view and manages the view’s contents. In the process, the view controller also manages the view’s subviews. But in most cases, the view’s frame is not set directly by the view controller. Instead, the view’s frame is determined by how the view controller’s view is displayed. More directly, it is configured by the object used to display it. Other conditions in the app, such as the presence of the status bar, can also cause the frame to change. Because of this, your view controller should be prepared to adjust the contents of its view when the view’s frame changes.
视图控制器有自己的视图并管理视图的内容。 在过程中,视图控制器还管理视图的子视图。 但是在大多数情况下,视图的框架并不是由视图控制器直接设置。相反,视图的框架由视图控制器的视图如何显示决定。更直接地说,它由显示它的对象来配置。 应用程序中的其它情况,比如状态栏的出现也可以导致框架发生改变。 因此,当视图的框架变化时,你的视图控制器应该准备好调整它的内容。
A Window Sets the Frame of Its Root View Controller’s View
一、窗口设置其根视图控制器视图的框架
The view associated with the window’s root view controller gets a frame based on the characteristics of the window. The frame set by the window can change based on a number of factors:
跟窗口的根视图控制器相关的视图根据窗口的特性来获取框架。 由窗口设置的框架会基于以下因素发生改变:
-
The frame of the window
- 窗口的框架
-
Whether or not the status bar is visible
- 状态栏是否可见
-
Whether or not the status bar is showing additional transient information (such as when a phone call is in progress)
- 状态栏是否显示额外的临时信息(比如来电)
-
The orientation of the user interface (landscape or portrait)
- 用户界面的朝向(水平或是垂直)
-
The value stored in the root view controller’s
wantsFullScreenLayout
property - 根视图控制器的
wantsFullScreenLayout 特性值
If your app displays the status bar, the view shrinks so that it does not
underlap the status bar. After all, if the status bar is opaque, there is no way
to see or interact with the content lying underneath it. However, if your app
displays a translucent status bar, you can set the value of your view
controller’swantsFullScreenLayout
property
to YES
to allow your view to be displayed full screen.
The status bar is drawn over the top of the view.
如果你的应用程序显示状态栏,视图缩小以便它不与状态栏重叠(underlap)。 毕竟如果状态栏不透明,我们没有办法查看或跟状态栏下面的内容想交互。 然而,如果你的程序显示一个半透明状态栏,你可以把视图控制器的wantsFullScreenLayout 特性设置为YES来让视图全屏显示。 状态栏覆盖在视图的顶部。
Full screen is useful when you want to maximize the amount of space available
for displaying your content. When displaying content under the status bar, place
that content inside a scroll view so that the user can scroll it out from under
the status bar. Being able to scroll your content is important because the user
cannot interact with content that is positioned behind the status bar or any
other translucent views (such as translucent navigation bars and toolbars).
Navigation bars automatically add a scroll content inset to your scroll view
(assuming it is the root view of your view controller) to account for the height
of the navigation bar; otherwise, you must manually modify the contentInset
property of your scroll view.
当你想要最大空间来显示你的内容时全屏很有用。
当在状态栏下面显示内容时,把那些内容放入一个滚动视图,这样用户滚动出在状态栏下面的内容。
让你的内容可以滚动是很重要的,因为用户不能跟位于状态栏或任何其它半透明的视图(比如半透明的导航栏和工具栏)后面的内容想交互。
导航栏自动添加一个滚动内容嵌入(inset
to)到你的滚动视图(假设它是你的视图控制器的根视图),用来占用(account)导航栏的高度。另外,你必须手动修改滚动视图的 contentInset特性。
A Container Sets the Frames of Its Children’s Views
二、容器设置其子女的视图框架
When a view controller is a child of a container view controller, its parent decides which children are visible. When it wants to show the view, it adds it as a subview in its own view hierarchy and sets its frame to fit it into its user interface. For example:
当一个视图控制器是一个容器视图控制器的子视图时,它的父视图决定哪些子视图可见。 当它想要显示视图时,它把它左右一个子视图添加到自己的视图层次结构并且设置它的框架让它适合用户界面。 比如:
-
A tab view controller reserves space at the bottom of its view for the tab bar. It sets the currently visible child’s view to use the remainder of the space.
- 标签视图控制器为标签栏保留了其视图底部空间。 它设置单前可见的子视图的视图来使用该剩余(remainder)空间。
-
A navigation view controller reserves space at the top for the navigation bar. If the currently visible child wants a navigation bar to be displayed, it also places a view at the bottom of the screen. The remainder of its view is given to the child to fill.
- 导航视图控制器为导航栏在顶部保留了空间。 如果当前可见的子视图想要显示一个导航栏,它还可以在屏幕的底部放置一个视图。 该视图的剩余空间可以让子视图来填充。
A child gets its frame from the parent all the way up to the root view controller, which gets its frame from the window.
一个子视图从其父视图那获取框架,一直延伸直到根视图控制器。根视图控制器的框架从窗口获取。
A Presented View Controller Uses a Presentation Context
三、一个被呈现的视图控制器使用一个陈述上下文
When a view controller is presented by another view controller, the frame it receives is based on the presentation context used to display the view controller. See“Presentation Contexts Provide the Area Covered by the Presented View Controller.”
当一个视图控制器由另一个视图控制器呈现时,它的框架基于用来显示视图控制器的陈述上下文(presentation context)。 查看“Presentation Contexts Provide the Area Covered by the Presented View Controller.”
A Popover Controller Sets the Size of the Displayed View
四、弹出控制器设置被显示视图的尺寸
A view controller displayed by a popover controller can determine the size of
its view’s area by setting its own contentSizeForViewInPopover
property value to the
size it wants. If the popover controller sets its own popoverContentSize
property to a different view
size, its size value overrides the view controller’s setting. To match the model
used by other view controllers, use the popover controller’s properties to
control its size and position.
由弹出控制器显示的视图控制器可以决定其视图区的尺寸,它把自己的contentSizeForViewInPopover特性值设置为需要的值。
如果弹出控制器把自己的 popoverContentSize 特性设置为一个不同的视图尺寸,它的尺寸值重写视图控制器的设置。
要想匹配其它视图控制器使用的模型,使用弹出控制器的特性来控制它的尺寸和位置。
How View Controllers Participate in the View Layout Process
五、视图控制器如何参与视图布局进程
When the size of a view controller’s view changes, its subviews are repositioned to fit the new space available to them. The views in the controller’s view hierarchy perform most of this work themselves through the use of layout constraints and autoresizing masks. However, the view controller is also called at various points so that it can participate in the process. Here’s what happens:
当一个图控制器的尺寸改变时,它的子视图将被重新定位以适应为它们的新空间。 视图控制器的视图层次中的视图大多数由自己执行该工作,它们通过使用布局常量和自动尺寸调整蒙版(autoresizing masks). 然而,视图控制器也在不同点被调用,这样它就可以参与进程。以下是发生的事情:
-
The view controller’s view is resized to the new size.
视图控制器的尺寸被重新设置为新尺寸。
-
If autolayout is not in use, the views are resized according to their autoresizing masks.
如果没有使用自动布局,视图根据它们的自动调整尺寸蒙版来调整尺寸。
-
The view controller’s
viewWillLayoutSubviews
method is called.调用视图控制器的viewWillLayoutSubviews
-
The view’s
layoutSubviews
method is called. If autolayout is used to configure the view hierarchy, it updates the layout constraints by executing the following steps:调用视图的
layoutSubviews 方法。如果使用了自动布局来配置视图层次,它通过执行以下步骤来更新布局常量。
-
The view controller’s
updateViewConstraints
method is called.调用视图控制器的
updateViewConstraints 方法。
-
The
UIViewController
class’s implementation of theupdateViewConstraints
method calls the view’supdateConstraints
method.实现UIViewController 类的updateViewConstraints 方法调用视图的
updateConstraints方法。
-
After the layout constraints are updated, a new layout is calculated and the views are repositioned.
布局常量更新以后,一个新布局被计算并且视图被重新定位。
-
-
The view controller’s
viewDidLayoutSubviews
method is called.调用视图控制器的viewDidLayoutSubviews 方法。
Ideally, the views themselves perform all of the necessary work to reposition themselves, without requiring the view controller to participate in the process at all. Often, you can configure the layout entirely within Interface Builder. However, if the view controller adds and removes views dynamically, a static layout in Interface Builder may not be possible. In this case, the view controller is a good place to control the process, because often the views themselves only have a limited picture of the other views in the scene. Here are the best approaches to this in your view controller:
理论上说,视图本身执行所有必要的工作来重新定位它们自己,而不需要视图控制器参与该过程。 你常常可以在界面生成器里配置整个布局。然而,如果视图控制器动态地添加和删除视图,界面生成器中的一个静态布局不可能完成该工作。在这种情况下,视图控制器是控制该过程的一个好地方,因为屏幕中视图本身常常只有其他视图的有限图片。 以下是在你的视图控制器上完成该工作的最好方法:
-
Use layout constraints to automatically position the views (iOS 6 and later). You override
updateViewConstraints
to add any necessary layout constraints not already configured by the views. Your implementation of this method must call[super updateViewConstraints]
.For more information on layout constraints, see Auto Layout Guide.
使用布局常量来自动定位视图(iOS 6 及以后版本). 重写updateViewConstraints 方法来添加任何必要的视图没有配置的布局常量。该方法的实现必须调用
[super updateViewConstraints]
. 更多布局常量的信息,请看 Auto Layout Guide. -
Use a combination of autoresizing masks and code to manually position the views (iOS 5.x). You override
layoutSubviews
and use it to reposition any views whose positions cannot be set automatically through the use of resizing masks.For more information on the autoresizing properties of views and how they affect the view, see View Programming Guide for iOS.
使用自动调整尺寸蒙版和代码的组合来手动定位视图(iOS 5.x). 重写layoutSubviews 方法并使用它来定位任何视图,这些视图的位置不能通过使用重新调整尺寸蒙版来自动设置。 更多关于视图的自动调整尺寸特性以及它们如何影响视图的信息,请看 View Programming Guide for iOS.
View Controller Programming Guide for iOS---(七),布布扣,bubuko.com