zoukankan      html  css  js  c++  java
  • Advanced Auto Layout:Changing Constraints

    Changing Constraints改变约束

    A constraint change is anything that alters the underlying mathematical expression of a constraint (see Figure 17-1). You can learn more about constraint equations in Anatomy of a Constraint.

    一个约束的改变是什么,改变了一个约束的基本数学表达式(参见图17-1)。在约束的解剖中,你可以了解更多有关约束方程的知识。

    Figure 17-1The constraint equationimage: ../Art/view_formula_2x.png

    All of the following actions change one or more constraints:下列操作改变一个或多个约束:

    • Activating or deactivating a constraint激活或停用约束

    • Changing the constraint’s constant value改变约束的常量值

    • Changing the constraint’s priority改变约束的优先级

    • Removing a view from the view hierarchy从视图层次结构中移除视图

    Other changes, like setting a control’s property, or modifying the view hierarchy, can also change constraints. When a change occurs, the system schedules a deferred layout pass (see The Deferred Layout Pass).

    其他更改,如设置控件属性或修改视图层次结构,也可以更改约束条件。当发生更改时,系统计划一个延迟的布局传递(见延迟布局通过)。

    In general, you can make these changes at any time. Ideally, most constraints should be set up in Interface Builder, or programatically created by the view controller during the controller’s initial setup (for example, in viewDidLoad). If you need to dynamically change constraints at runtime, it’s usually best to change them when the application’s state changes. For example, if you want to change a constraint in response to a button tap, make that change directly in the button’s action method. 

    一般来说,你可以随时做出这些改变。理想的情况是,大多数的限制应该在界面生成器设置,或通过编程创建的视图控制器的初始安装过程中(例如,在viewDidLoad)。如果在运行时需要动态更改约束,通常在应用程序状态改变时更改它们。例如,如果你想改变一个约束响应按钮水龙头,使这一变化直接在按钮的行动方法。

    Occasionally, you may need to batch a set of changes for performance reasons. For more information, see Batching Changes.

    有时,您可能需要为性能原因批量设置一组更改。有关更多信息,请参见批处理更改。

    The Deferred Layout Pass延迟布局通过

    Instead of immediately updating the affected views’ frames, Auto Layout schedules a layout pass for the near future. This deferred pass updates the layout’s constraints and then calculates the frames for all the views in the view hierarchy. 

    而不是立即更新受影响的视图的框架,自动布局时间表布局通行证为不久的将来。此延迟传递更新布局的约束,然后计算视图层次结构中所有视图的帧。

    You can schedule your own deferred layout pass by calling the setNeedsLayout method or the setNeedsUpdateConstraints method.

    你可以安排自己的递延布局通过调用setneedslayout法或setneedsupdateconstraints方法。

    The deferred layout pass actually involves two passes through the view hierarchy:延迟布局传递实际上涉及两个通过视图层次结构的传递:

    1. The update pass updates the constraints, as necessary更新通道根据需要更新约束

    2. The layout pass repositions the view’s frames, as necessary通过重新定位布局视图的框架,是必要的

    Update Pass更新过程

    The system traverses the view hierarchy and calls the updateViewConstraints method on all view controllers, and the updateConstraints method on all views. You can override these methods to optimize changes to your constraints (see Batching Changes).

    系统遍历视图层次,呼吁所有的视图控制器的updateviewconstraints方法和updateconstraints方法对所有视图。可以重写这些方法以优化对约束的更改(参见批处理更改)。

    Layout Pass布局传

    The system traverses the view hierarchy again and calls viewWillLayoutSubviews on all view controllers, and layoutSubviews (layout in OS X) on all views. By default, the layoutSubviews method updates the frame of each subview with the rectangle calculated by the Auto Layout engine. You can override these methods to modify the layout (see Custom Layouts).

    系统遍历视图层次再次呼吁所有的视图控制器和viewwilllayoutsubviews,layoutSubviews(在OS X上的所有视图的布局)。默认情况下,layoutSubviews方法的自动布局引擎计算矩形更新每个子视图的框架。您可以重写这些方法来修改布局(参见自定义布局)。

    Batching Changes配料的变化

    It is almost always cleaner and easier to update a constraint immediately after the affecting change has occurred. Deferring these changes to a later method makes the code more complex and harder to understand.

    它几乎总是干净和更容易更新约束后立即发生影响变化。将这些更改推迟到以后的方法会使代码更加复杂和难以理解。

    However, there are times when you may want to batch changes for performance reasons. This should only be done when changing the constraints in place is too slow, or when a view is making a number of redundant changes.

    然而,有时您可能需要批量更改性能原因。这只能在改变约束的时候太慢,或者当视图正在进行一些冗余更改时完成。

    To batch a change, instead of making the change directly, call the  setNeedsUpdateConstraints method on the view holding the constraint. Then, override the view’s updateConstraints method to modify the affected constraints.

    批量更改,而不是直接做出改变,呼吁观约束的setneedsupdateconstraints方法。然后,重写视图的updateconstraints法修改影响的约束。

    NOTE

    Your updateConstraints implementation must be as efficient as possible. Do not deactivate all your constraints, then reactivate the ones you need. Instead, your app must have some way of tracking your constraints, and validating them during each update pass. Only change items that need to be changed. During each update pass, you must ensure that you have the appropriate constraints for the app’s current state. 

    你的updateconstraints实施必须尽可能高效。不要关闭你所有的约束,然后激活你需要的。相反,你的应用程序必须有一些方法来跟踪你的约束,并在每次更新过程中验证它们。只更改需要更改的项目。在每次更新过程中,必须确保对应用程序的当前状态有适当的约束。

    Always call the superclasses implementation as the last step of your updateConstraints method’s implementation.

    总是调用父类实现的最后一步,你的updateconstraints方法的实现。

    Do not call setNeedsUpdateConstraints inside your updateConstraints method. Calling setNeedsUpdateConstraints schedules another update pass, creating a feedback loop.

    不要叫setneedsupdateconstraints在你updateconstraints方法。打电话setneedsupdateconstraints安排另一个更新的传球,创造一个反馈回路。

    Custom Layouts自定义布局

    Override the viewWillLayoutSubviews or layoutSubviews methods to modify the results returned by the layout engine.

    viewwilllayoutsubviews layoutSubviews方法重写或修改的布局引擎返回的结果。

    IMPORTANT重要

    If possible, use constraints to define all of your layouts. The resulting layouts are more robust and easier to debug. You should only override the the viewWillLayoutSubviews or layoutSubviews methods when you need to create a layout that cannot be expressed with constraints alone.

    如果可能,请使用约束来定义所有布局。由此产生的布局更强大,更容易调试。你应该只覆盖了viewwilllayoutsubviews layoutSubviews方法或当你需要创建一个布局,不能单独表达的限制。

    When overriding these methods, the layout is in an inconsistent state. Some views have already been laid out. Others have not. You need to be very careful about how you modify the view hierarchy, or you can create feedback loops. The following rules should help you avoid feedback loops:

    当重写这些方法时,布局处于不一致状态。已经提出了一些观点。别人没有。您需要非常小心,您如何修改视图层次结构,或者您可以创建反馈循环。下面的规则应该帮助你避免反馈循环:

    • You must call the superclass’s implementation somewhere in your method.你必须调用父类的方法实现在你的地方。

    • You can safely invalidate the layout of views in your subtree; however, you must do this before you call the superclass’s implementation.你可以在你的树视图无效的布局;但是,你必须这样做,在你调用父类的实现。

    • Don’t invalidate the layout of any views outside your subtree. This could create a feedback loop.不要使您的子树以外的任何视图布局无效。这可以创建一个反馈循环。

    • Don’t call setNeedsUpdateConstraints. You have just completed an update pass. Calling this method creates a feedback loop.别叫setneedsupdateconstraints。您刚刚完成了更新通行证。调用此方法创建一个反馈循环。

    • Don’t call setNeedsLayout. Calling this method creates a feedback loop.别叫setneedslayout。调用此方法创建一个反馈循环。

    • Be careful about changing constraints. You don’t want to accidentally invalidate the layout of any views outside your subtree.注意改变约束。您不想意外地使子树外的视图布局无效。

    iOS Android Appcan WeChat
  • 相关阅读:
    从零到一,使用实时音视频 SDK 一起开发一款 Zoom 吧
    七牛云助你度寒冬 | 每天 10:24, 新用户抢全额免单
    (下)挖掘传统行业日志大数据的无限价值
    (上)挖掘传统行业日志大数据的无限价值
    8.27 直播| 挖掘传统行业日志大数据的无限价值
    千亿级数量下日志分析系统的技术架构选型
    七牛云工程效率部测试服务如何为 70 万+ 客户保驾护航?
    云计算的新墨菲定律
    如何建设高吞吐量的日志平台
    新一代智能视频云发展现状分析:五大要素成关键
  • 原文地址:https://www.cnblogs.com/zyingn/p/AdvancedAutoLayout__ChangingConstraints.html
Copyright © 2011-2022 走看看