美文网首页
你不知道的自动布局

你不知道的自动布局

作者: rxdxxxx | 来源:发表于2016-04-09 13:49 被阅读36次
  • autoresizingMask转换为自动约束
@interface UIView (UIConstraintBasedCompatibility) 

/* By default, the autoresizing mask on a view gives rise to constraints that fully determine 
 the view's position. This allows the auto layout system to track the frames of views whose 
 layout is controlled manually (through -setFrame:, for example).
 When you elect to position the view using auto layout by adding your own constraints, 
 you must set this property to NO. IB will do this for you.
 */
@property(nonatomic) BOOL translatesAutoresizingMaskIntoConstraints NS_AVAILABLE_IOS(6_0); // Default YES

/* constraint-based layout engages lazily when someone tries to use it (e.g., adds a constraint to a view).  If you do all of your constraint set up in -updateConstraints, you might never even receive updateConstraints if no one makes a constraint.  To fix this chicken and egg problem, override this method to return YES if your view needs the window to use constraint-based layout.  
 */
+ (BOOL)requiresConstraintBasedLayout NS_AVAILABLE_IOS(6_0);

@end

  • 对于多行 UILabel 需要设置 preferredMaxLayoutWidth
// Support for constraint-based layout (auto layout)
// If nonzero, this is used when determining -intrinsicContentSize for multiline labels
@property(nonatomic) CGFloat preferredMaxLayoutWidth NS_AVAILABLE_IOS(6_0);
  • 例子:
- (void)layoutSubviews {
    [super layoutSubviews];

    // for multiline UILabel's you need set the preferredMaxLayoutWidth
    // you need to do this after [super layoutSubviews] as the frames will have a value from Auto Layout at this point

    // stay tuned for new easier way todo this coming soon to Masonry

    CGFloat width = CGRectGetMinX(self.shortLabel.frame) - kPadding.left;
    width -= CGRectGetMinX(self.longLabel.frame);
    self.longLabel.preferredMaxLayoutWidth = width;

    // need to layoutSubviews again as frames need to recalculated with preferredLayoutWidth
    [super layoutSubviews];
}

相关文章

  • 你不知道的自动布局

    autoresizingMask转换为自动约束 对于多行 UILabel 需要设置 preferredMaxLay...

  • 【OC梳理】自动布局

    自动布局基础篇 关于自动布局的基本使用,参考网上的文章即可,如:iOS开发-自动布局篇:史上最牛的自动布局教学! ...

  • iOS开发之 自动布局

    iOS开发之自动布局AutoLayout 目录: 1 iOS自动布局简介2 iOS自动布局AutoLayout(代...

  • iOS 布局方案

    布局方案 绝对布局 自动布局

  • 初见FLEX

    FLEX布局 一种新的布局方式,flex布局 flex布局与方向无关 flex布局可以实现空间自动分配、自动对齐。...

  • 通过storyboard来自动布局ScrollView(不依靠代

    在做自动布局之前首先大家都知道一个自动布局,自动布局在我理解就是为了适配任何的屏幕。每一个控件的自动布局都是为了确...

  • 最轻巧的自动布局--ZXPAutoLayout框架

    最轻巧的自动布局--ZXPAutoLayout框架 最轻巧的自动布局--ZXPAutoLayout框架

  • Flex——告别CSS布局

    Flex 布局可以实现空间自动分配、自动对齐Flex 适用于简单的线性布局,复杂布局使用 Grid 布局注意:设为...

  • IOS开发 自动布局子视图

    本节学习内容: 1.自动子视图布局的概念 2.自动布局视图的创建 3.自动布局子视图的实现 【ViewContro...

  • iOS原生布局简介

    自动布局核心公式 自动布局构造函数 自动布局类函数 VFL可视化格式语言 H 水平方向 V 垂直方向 | 边界 [...

网友评论

      本文标题:你不知道的自动布局

      本文链接:https://www.haomeiwen.com/subject/fcfblttx.html