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 the?updateViewConstraints
?method calls the view’s?updateConstraints
?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.?