Custom View Which Support AutoLayout

创建支持AutoLayout的Custom View

AutoLayout 通过使view更加的自组织来减轻controller类的负担。

当实现custom view类时,需要提供足够的信息来使AutoLayout系统能够正确计算和满足约束(Constraints)。

1. 为Custom View指定 intrinsic size (内在的/固有的 size)

"Leaf-level views, such as buttons, typically know more about what size they should be than does

the code that is positioning them. This is communicated through the method intrinsicContentSize,

which tells the layout system that there is some content it doesn’t natively understand in a view, and

which provides to the layout system the intrinsic size of that content." Ref[1]

"A view can implement intrinsicContentSize to return absolute values for its width and height,

or to return NSViewNoInstrinsicMetric for either or both to indicate that it has no intrinsic metric

for a given dimension." Ref[1]

"Remember that you can also set constant width or height constraints on any view, and you don't

need to override instrinsicContentSize if these dimensions won't be changing with changing view content."

UIView的instrinsicContentSize默认实现是返回(UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric)。

2. 通知AutoLayout关于 intrinsic size 的变化

当Custom View的内在Size发生变化时通知AutoLayout,通过方法invalidateIntrinsicContentSize来通知AutoLayout。

3. AutoLayout操作UIView的"Alignement Rects"而非frame

Constraints do not actually relate the frames of the views, rather they relate the "alignment rects" of views.

 /* Set the NSUserDefault UIViewShowAlignmentRects to YES to see alignment rects drawn.
*/
- (CGRect)alignmentRectForFrame:(CGRect)frame NS_AVAILABLE_IOS(6_0);
- (CGRect)frameForAlignmentRect:(CGRect)alignmentRect NS_AVAILABLE_IOS(6_0); /* override this if the alignment rect is obtained from the frame by insetting each edge by a fixed amount.
This is only called by alignmentRectForFrame: and frameForAlignmentRect:.
*/
- (UIEdgeInsets)alignmentRectInsets NS_AVAILABLE_IOS(6_0);

在开发时使"alignment rects"显示出来:

"

  • In the Xcode menu Product -> Scheme -> Edit Scheme (or ⌘-<)
  • In Debug, the Arguments tab, Arguments passed on launch, click the plus button and enter this:

    -UIViewShowAlignmentRects YES (for iOS projects)
    or
    -NSViewShowAlignmentRects YES (for Mac OS projects)

  • You'll need the dash at the start
  • Tap OK

You should have a bunch of yellow outlines when you run" Ref[3]

4. Demo for Mac OS X App

Ref[2]


Reference

1. Implementing a Custom View to Work with Auto Layout

2. Custom Views with Auto Layout

http://www.electricpeelsoftware.com/2013/05/08/custom-views-with-auto-layout.html

https://github.com/peterstuart/AutoLayoutExample

3. How to set NSViewShowAlignmentRects?

http://stackoverflow.com/questions/15394033/how-to-set-nsviewshowalignmentrects

4. Advanced Auto Layout Toolbox (AAAA+)

https://www.objc.io/issues/3-views/advanced-auto-layout-toolbox/

iOS.AutoLayout.2.CustomView-with-AutoLayout的更多相关文章

  1. iOS: 在代码中使用Autolayout (2) – intrinsicContentSize和Content Hugging Priority【转】

    原文:http://www.mgenware.com/blog/?p=491 接上文:iOS: 在代码中使用Autolayout (1) – 按比例缩放和优先级. 我们继续来看在代码中使用Autola ...

  2. 转:iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解

    1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS开发者基 ...

  3. iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解

    === 1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS ...

  4. iOS自动布局学习(UIView+AutoLayout)

    自动布局虽然在iOS6的时候已经推出,不过由于各个原因并没有被开发组广泛使用.一方面是大家的app支持版本都是低于iOS6的,另一方面来说是Xcode支持木有现在这么好.以前由于iPhone设备相对固 ...

  5. iOS 8 自动布局sizeclass和autolayout的基本使用

    1.首先创建新的工程,设置rootviewcontroller(这里不再多说) 2.勾选下面(因为我们到下面是使用sizeClass,所以勾选两个): 3.这里我创建了一个lable,名称为View1 ...

  6. iOS编程(双语版)-视图-Autolayout代码初步

    一谈到Autolayout,初学者肯定想到的是IB中使用拖拽啊,pin啊各种鼠标操作来进行添加各种约束. 今天我们要聊得是如何利用代码来添加视图间的约束. 我们来看一个例子: (Objective-C ...

  7. iOS UI-自动布局(AutoLayout)

    // // ViewController.m // IOS_0115_AutoLayout // // Created by ma c on 16/1/15. // Copyright (c) 201 ...

  8. 从此爱上iOS Autolayout

    转:从此爱上iOS Autolayout 这篇不是autolayout教程,只是autolayout动员文章和经验之谈,在本文第五节友情链接和推荐中,我将附上足够大家熟练使用autolayout的教程 ...

  9. iOS开发~UI布局(三)深入理解autolayout

    一.概要 通过对iOS8界面布局的学习和总结,发现autolayout才是主角,autolayout是iOS6引入的新特性,当时还粗浅的学习了下,可是没有真正应用到项目中.随着iOS设备尺寸逐渐碎片化 ...

  10. iOS:自动布局Autolayout

    自动布局:Autolayout 简介: 在以前的iOS程序中,是如何设置布局UI界面的? 经常编写大量的坐标计算代码 为了保证在3.5 inch和4.0 inch屏幕上都能有完美的UI界面效果,有时还 ...

随机推荐

  1. tensorflow降低版本

    tensorflow降低版本: pip install tensorflow==1.2.0 查看版本: import tensorflow as tf print(tf.__version__)

  2. 历届试题 大臣的旅费-(树的直径+dfs)

    问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首 ...

  3. java-学习1

    作为一个想要深入的程序猿,只是学习前端是不够的,我总结我的前端工作是围绕着html.css.js展开写的再好也是展现在表面,所以 我想学习一门能够深入的后台语言,想来想去我还是选择java作为以后深入 ...

  4. ie兼容,手机端兼容问题

    兼容性: 1.ie6,7不能兼容border-radius:若需要可以用图片的方式进行模拟. 2.ie6, 7中如果兄弟元素没有给左浮动,而本身给了右浮动,将会出现塌陷(也就是掉下去):如需要可以将右 ...

  5. java.lang.IllegalArgumentException: Missing either @POST URL or @Url parameter.

    以前联调的接口,都是类似这样子的http://ip:8080/WLInterface/register   在baseUrl(http://ip:8080/WLInterface/register ) ...

  6. Java 获取一个字符串中,另一个字符串出现的次数

    Java 获取一个字符串中,另一个字符串出现的次数 思想: 1. indexOf到字符串中到第一次出现的索引2. 找到的索引+被找字符串长度,截取字符串3. 计数器++ 代码实现: public cl ...

  7. react+webpack+babel环境搭建

    [react+webpack+babel环境搭建] 1.react官方文档推荐使用 babel-preset-react.babel-preset-es2015 两个perset. Babel官方文档 ...

  8. getHibernateTemplate()的用法 (转)

    在spring 中获得由spring所配置的hibernate的操作对象,然后利用此对象进行,保存,修改和删除等操作, 此方法是在配置了spring以后,hibernate由spring接管,不直接使 ...

  9. 消除游戏源码 Match 3 Jewel Full 298 Levels

    Match 3 Jewel Full 298 Levels 一款unity3d编写的消消乐游戏, 关卡丰富,很好玩 下载地址:点击下载

  10. jQuery MD5 加密

    一 MD5 概述 MD5消息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value) ...