Debugging in Code

有两个调试layout问题的阶段。

1. Map from “this view is in the wrong place” to “this constraint (or these constraints) is (are) incorrect.”

2. Map from “this constraint is incorrect” to “this code is incorrect.”

To debug a problem with Auto Layout

  1. Identify the view with an incorrect frame.
    It may be obvious which view has the problem; if it is not, you may find it helpful to use the NSView

    method _subtreeDescription to create a textual description of the view hierarchy.

  2. If possible, reproduce the issue while running under the Auto Layout template in Instruments.

  3. Find the bad constraint or constraints.

    To get the constraints affecting a particular view, use
    constraintsAffectingLayoutForOrientation: in OS X or
    constraintsAffectingLayoutForAxis: in iOS.

    4.If it’s not obvious which constraint is wrong at this point, visualize the constraints on screen by passing the constraints to the window using visualizeConstraints:.

    When you click a constraint, it is printed in the console. In this way you can you can determine which is which, and typically identify which is incorrect.

    At this point you may be informed that the layout is ambiguous.

  5. Find the code that’s wrong.
    Sometimes, once you have identified the incorrect constraint, you will know what to do to fix it.

    If this is not the case, then use Instruments to search for the pointer of the constraint (or some of its
description). This will show you interesting events in that constraint’s lifecycle—its creation, modification,
installation into windows, and so on. For each of these you can see the backtrace where it happened.
Find the stage at which things went awry, and look at the backtrace. This is the code with the problem.

Auto Layout by Example

Auto Layout 使得自动解决复杂的layout问题很容易。而且不需要手动的操作。通过创建正确的约束,你可以创建在代码中难以掌控的layout。例如同等空间的view调整适应方向或者大小的变动。scroll view中作用于滚动内容大小的元素。或者scroll view中不滚动的元素。

Using Scroll Views with Auto Layout

当你在app中使用auto layout时,scroll view将会是一个挑战。滚动内容大小必须设置正确用户才能够滚动到可视区域。例如,如果你需要为了地图缩放和说明在scroll view的顶部设定一个contextual view,是很难确定这些元素不会跟着其他的content一起滚动。

Controlling Scroll View Content Size

scroll view的content大小取决于他的子节点的约束。

To set the size of a scroll view 如何设置scroll view的大小

  1. Create the scroll view.创建scroll view

  2. Place the UI element inside it. 在scroll view中放置UI元素

  3. Create constraints that fully define the width and height of the scroll view content.创建能够定义scroll view content高度和宽度的约束

必须保证为scroll view中的每一个子view都定义了约束。例如,当为一个没有固定content size的view定义约束时,你需要不仅仅定义一个leading edge约束,你必须也创建trailing edge, width, 和高度约束。不能缺少从一个scroll view edge到其他的约束。

Creating Anchored Views Inside a Scroll View 在scroll view中创建固定的views

你可能发现有时候你需要在scroll view中创建一块无法滚动的区域。你通过创建一个container view来完成这个目标。

To lock a view inside a scroll view

  1. Create a container view to hold the scroll view.  创建一个container view来保存这个scroll view

  2. Create the scroll view and place it in the container view with all edges equal to zero points. 创建一个scroll view并且把他放置在所有的edges都是0的container view中

  3. Create and place a subview inside of the scroll view. 在scroll view中创建并放置一个subview

  4. Create constraints from the subview to the container view.创建这个subview痛container view之间的约束

以下是例子的具体执行步骤:

首先,创建一个container view来包含之后的scroll view。把container view大小设置为想要的scroll view的大小。

然后,在这个container view中创建一个scroll view,并且重新规定scroll view的大小所有的边框都覆盖到container view中,设置distance为0

然后创建一个text view到scroll view中。

当放置好text view后,创建从text view到container view的约束。创建约束讲text view锚到container view中。确保scroll view不会滚动这个text view。

为了在view层中创建一个贯穿多个view的约束,一般简单的方法是选择这个view,然后ctrl+drag到interface builder outline view的container view中去。

在约束的覆盖图中,设置想要的约束。

Autolayout 03的更多相关文章

  1. This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.

    -- :::] This application is modifying the autolayout engine from a background thread, which can lead ...

  2. [BS] 小知识点总结-03

    1.Autolayout中“constrain to margins” Autolayout中的页面边距的问题ios8以后的UIView增加了layoutMargins属性,在Storyboard/I ...

  3. Android游戏开发实践(1)之NDK与JNI开发03

    Android游戏开发实践(1)之NDK与JNI开发03 前面已经分享了两篇有关Android平台NDK与JNI开发相关的内容.以下列举前面两篇的链接地址,感兴趣的可以再回顾下.那么,这篇继续这个小专 ...

  4. Java多线程系列--“JUC锁”03之 公平锁(一)

    概要 本章对“公平锁”的获取锁机制进行介绍(本文的公平锁指的是互斥锁的公平锁),内容包括:基本概念ReentrantLock数据结构参考代码获取公平锁(基于JDK1.7.0_40)一. tryAcqu ...

  5. 使用Autolayout实现UITableView的Cell动态布局和高度动态改变

    本文翻译自:stackoverflow 有人在stackoverflow上问了一个问题: 1 如何在UITableViewCell中使用Autolayout来实现Cell的内容和子视图自动计算行高,并 ...

  6. iOS系列 基础篇 03 探究应用生命周期

    iOS系列 基础篇 03 探究应用生命周期 目录: 1. 非运行状态 - 应用启动场景 2. 点击Home键 - 应用退出场景 3. 挂起重新运行场景 4. 内存清除 - 应用终止场景 5. 结尾 本 ...

  7. Masonry介绍与使用实践:快速上手Autolayout

    1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时代 w ...

  8. javaSE基础03

    javaSE基础03 生活中常见的进制:十进制(0-9).星期(七进制(0-6)).时间(十二进制(0-11)).二十四进制(0-23) 进制之间的转换: 十进制转为二进制: 将十进制除以2,直到商为 ...

  9. 利用代码添加autolayout约束

    1.概述 通常我们通过storyboard能够完成的,代码也能够完成,所以这里介绍下代码实现约束的添加,通常我们不这么干(在不使用第三方框架的情况下,使用系统自带的类添加约束特别繁琐),所以这里仅仅简 ...

随机推荐

  1. 网络抢票黄牛,大部分是骗人的。公布一个骗钱黄牛,QQ:2233261390,QQ群:29443597,支付页面:https://me.alipay.com/q336

    想着给女友买张回广州的回程火车票,抢了3天也没弄到.情急之下找了网络上所谓的黄牛.结果上当受骗.具体经过是这样的: 对方承诺抢到再付款.于是等他抢到后截图给我看,而且可以远程到他的机器去看,我也确实远 ...

  2. centos使用--vsftpd配置

    目录 1 在服务器配置FTP服务 1.1 在root权限下,通过如下命令安装Vsftp(以CentOS系统为例): 1.2 在启动vsftpd服务之前,需要登录云服务器修改配置文件,禁用匿名登录. 1 ...

  3. IOS开发---菜鸟学习之路--(二十)-二维码扫描功能的实现

    本章将讲解如何实现二维码扫描的功能 首先在github上下载ZBar SDK地址https://github.com/bmorton/ZBarSDK 然后将如下的相关类库添加进去 AVFoundati ...

  4. leetcode 【 Minimum Path Sum 】python 实现

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  5. 使用 NPC,NPCManager 在 XNA 中创建 NPC(十九)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  6. PostgreSQL 如何优化索引效率

    使用 gin() 创建全文索引后,虽然有走索引,但是当结果集很大时,查询效率还是很底下, SELECT keyword,avg_mon_search,competition,impressions,c ...

  7. 【转】MapReduce:默认Counter的含义

    MapReduce Counter为提供我们一个窗口:观察MapReduce job运行期的各种细节数据.今年三月份期间,我曾经专注于MapReduce性能调优工作,是否优化的绝大多评估都是基于这些C ...

  8. Leetcode 523.连续的子数组和

    连续的子数组和 给定一个包含非负数的数组和一个目标整数 k,编写一个函数来判断该数组是否含有连续的子数组,其大小至少为 2,总和为 k 的倍数,即总和为 n*k,其中 n 也是一个整数. 示例 1: ...

  9. js跨域post请求

    function funPostBack(srvMethod){ /* var contentNR=$(document.getElementById("reportFrame") ...

  10. Spring Cloud 目录

    Spring Cloud Eureka Spring Cloud Config Spring Cloud Feign Spring Cloud Hystrix Spring Cloud Ribbon ...