博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Displaying Your Views at Runtime
阅读量:5146 次
发布时间:2019-06-13

本文共 4364 字,大约阅读时间需要 14 分钟。

UIKit automatically loads views from your storyboard file when they are needed. As part of the loading process, UIKit performs the following sequence of tasks:

    1. Instantiates views using the information in your storyboard file. 
    2. Connects all outlets and actions. 
    3. Assigns the root view to the view controller’s property. 
    4. Calls the view controller’s method.
      When this method is called, the view controller’s trait collection is empty and views may not be in their final positions. 
    5. Calls the view controller’s method.
      Use this method to add or remove views, modify layout constraints, and load data for your views

 

Before displaying a view controller’s views onscreen, UIKit gives you some additional chances to prepare those views before and after they are onscreen. Specifically, UIKit performs the following sequence of tasks:

  1. Calls the view controller’s method to let it know that its views are about to appear onscreen. 
  2. Updates the layout of the views. 
  3. Displays the views onscreen. 
  4. Calls the method when the views are onscreen. 

 

When you add, remove, or modify the size or position of views, remember to add and remove any constraints that apply to those views. Making layout-related changes to your view hierarchy causes UIKit to mark the layout as dirty. During the next update cycle, the layout engine computes the size and position of views using the current layout constraints and applies those changes to the view hierarchy.

 

 

Managing View Layout

When the size and position of views changes, UIKit updates the layout information for your view hierarchy. For views configured using Auto Layout, UIKit engages the Auto Layout engine and uses it to update the layout according to the current constraints. UIKit also lets other interested objects, such as the active presentation controller, know abut the layout changes so that they can respond accordingly.

During the layout process, UIKit notifies you at several points so that you can perform additional layout-related tasks. Use these notifications to modify your layout constraints or to make final tweaks to the layout after the layout constraints have been applied. During the layout process, UIKit does the following for each affected view controller:

    1. Updates the trait collections of the view controller and its views, as needed; see  
    2. Calls the view controller’s method. 
    3. Calls the method of the current object. 
    4. Calls the method of view controller’s root view.
      The default implementation of this method computes the new layout information using the available constraints. The method then traverses the view hierarchy and calls layoutSubviews for each subview. 
    5. Applies the computed layout information to the views. 
    6. Calls the view controller’s method. 
    7. Calls the method of the current object.

 

View controllers can use the viewWillLayoutSubviews and viewDidLayoutSubviews methods to perform additional updates that might impact the layout process. Before layout, you might add or remove views, update the size or position of views, update constraints, or update other view-related properties. After layout, you might reload table data, update the content of other views, or make final adjustments to the size and position of views.

Here are some tips for managing your layout effectively:

  • Use Auto Layout. The constraints you create using Auto Layout are a flexible and easy way to position your content on different screen sizes. 
  • Take advantage of the top and bottom layout guides. Laying out content to these guides ensures that your content is always visible. The position of the top layout guide factors in the height of the status bar and navigation bar. Similarly, the position of the bottom layout guide factors in the height of a tab bar or toolbar. 
  • Remember to update constraints when adding or removing views. If you add or remove views dynamically, remember to update the corresponding constraints. 
  • Remove constraints temporarily while animating your view controller’s views. When animating views using UIKit Core Animation, remove your constraints for the duration of the animations and add them back when the animations finish. Remember to update your constraints if the position or size of your views changed during the animation. 

For information about presentation controllers and the role they play in the view controller architecture, see .

 

转载于:https://www.cnblogs.com/cocoabanana/p/5554704.html

你可能感兴趣的文章
帮助你在 Photoshop 中轻松实现长阴影效果的工具
查看>>
hdu 4602 递推关系矩阵快速幂模
查看>>
[Dynamics 365] 关于Currency的一点随笔
查看>>
【Android】activity-alias的使用
查看>>
熟悉常用的HDFS操作
查看>>
Hadoop综合大作业
查看>>
GDB学习之道:GDB调试精粹及使用实例
查看>>
【Android】Android实现截取当前屏幕图片并保存至SDCard
查看>>
人工智能-智能创意平台架构成长之路(一)--长篇开篇
查看>>
CVE-2019-0708漏洞检测工具
查看>>
CSS最常用和实用的技巧
查看>>
系统设计基础第六周学习总结
查看>>
leetcode 11 Container with Most Water
查看>>
Linux目录结构
查看>>
PLSQLDeveloper_免安装自带client
查看>>
python三级联动
查看>>
CCString 类
查看>>
1505: 酷酷的单词
查看>>
小白实习趣事
查看>>
nginx正向代理SFTP整体配置方案
查看>>