UIView 中自定布局的 api


// Allows you to perform layout before the drawing cycle happens. -layoutIfNeeded forces layout early

// 允许你在绘制循环之前完成布局,  layoutIfNeeded 会提前点发生。

/*

    使 view 的当前约束作废,在下一更新循环(view 的绘制)进行约束更新。

    当你想要调整view 的subview 的约束的时候。在 app 的主线程中调用这个方法。这个方法是 请求和实现的标签,这个方法影响不会的实现不会立即调用,而是在下一次更新循环中执行。

*/ 

- (void)setNeedsLayout;

/*

    立即实现自动布局

    只用这个方法在绘制之前实现自动布局,这个方法会将布局信息发送到 root view ,从 root view 开始布局。

*/

- (void)layoutIfNeeded;

/*

    override point. called by layoutIfNeeded automatically.

    重载这个方法,通过 layoutIfNeeded 来自动调用这个方法。

    As of iOS 6.0, when constraints-based layout is used the base implementation applies the constraints-based layout, otherwise it does nothing.

   只有实现了这个方法,自动布局会根据这实现提供的布局进行布局。否则就什么也不会发生。

    在 iOS5.1 之前这个方法什么也没做,否则,这个默认实现会使用任何已经设置的约束去确定 subview 的尺寸和位置。

    子类能够重载这个方法去完成子类的子视图的精确布局。

    只有在子视图的 autoresizing 和 constraint-based 提供的布局行为不是你想要的时候你才重载这个方法。    

    你能够使用这个方法的实现,直接设置你 subview 的frame。

    

    你不能直接调用这个方法,如果你想要约束进行更新。

    你应该调用 `setNeedsLayout` 方法,在下一次视图绘制之前更新约束。

    如果你想view 的layout 的约束更新立即实现。你也可以调用 `layoutIfNeeded` 方法。

*/ 

- (void)layoutSubviews;

/*

 -layoutMargins returns a set of insets from the edge of the view's bounds that denote a default spacing for laying out content.

 If preservesSuperviewLayoutMargins is YES, margins cascade down the view tree, adjusting for geometry offsets, so that setting the left value of layoutMargins on a superview will affect the left value of layoutMargins for subviews positioned close to the left edge of their superview's bounds

 If your view subclass uses layoutMargins in its layout or drawing, override -layoutMarginsDidChange in order to refresh your view if the margins change.

 */

// 布局间隙

@property (nonatomic) UIEdgeInsets layoutMargins NS_AVAILABLE_IOS(8_0);

// 是否保留父视图的布局间隙

@property (nonatomic) BOOL preservesSuperviewLayoutMargins NS_AVAILABLE_IOS(8_0); // default is NO - set to enable pass-through or cascading behavior of margins from this view’s parent to its children

// 布局间隙指导

- (void)layoutMarginsDidChange NS_AVAILABLE_IOS(8_0);

/* The edges of this guide are constrained to equal the edges of the view inset by the layoutMargins

 */

@property(readonly,strong) UILayoutGuide *layoutMarginsGuide NS_AVAILABLE_IOS(9_0);

/// This content guide provides a layout area that you can use to place text and related content whose width should generally be constrained to a size that is easy for the user to read. This guide provides a centered region that you can place content within to get this behavior for this view.

@property (nonatomic, readonly, strong) UILayoutGuide *readableContentGuide NS_AVAILABLE_IOS(9_0);


//

// UIView Constraint-based Layout Support

//

typedef NS_ENUM(NSInteger, UILayoutConstraintAxis) {

     // 水平轴

     UILayoutConstraintAxisHorizontal = 0,

     // 竖直轴

     UILayoutConstraintAxisVertical = 1

};

// Installing Constraints ( 约束安装 )

/* A constraint is typically installed on the closest common ancestor of the views involved in the constraint.

    一个约束通常是安装在最近的共同祖先的视图。

     It is required that a constraint be installed on _a_ common ancestor of every view involved. 

    The numbers in a constraint are interpreted in the coordinate system of the view it is installed on. 

    A view is considered to be an ancestor of itself.

 */

@interface UIView (UIConstraintBasedLayoutInstallingConstraints)

@property(nonatomic,readonly) NSArray<__kindof NSLayoutConstraint *> *constraints NS_AVAILABLE_IOS(6_0);

// 添加一个约束

// This method will be deprecated in a future release and should be avoided. Instead, set NSLayoutConstraint's active property to YES.

// 这个方法会在将来的发布版本中被弃用,请避免使用。

// 替代, 设置  NSLayoutConstraint's 的 active 的属性为 yes.

- (void)addConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0); 

// 添加多个约束

// This method will be deprecated in a future release and should be avoided. Instead use +[NSLayoutConstraint activateConstraints:].

// 这个方法将会在将来的发布版本中弃用,请避免使用。

// 替代,  +[NSLayoutConstraint activateConstraints:].

- (void)addConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints NS_AVAILABLE_IOS(6_0); 

// 移除一个约束

// This method will be deprecated in a future release and should be avoided. Instead set NSLayoutConstraint's active property to NO.

// 这个方法会在将来的发布版本中弃用,请避免使用。

// 替代,设置 NSLayoutConstraint's 的 active 的属性为 no.

- (void)removeConstraint:(NSLayoutConstraint *)constraint NS_AVAILABLE_IOS(6_0); 

// 移除多个约束

// This method will be deprecated in a future release and should be avoided. Instead use +[NSLayoutConstraint deactivateConstraints:].

// 这个方法会在将来的发布版本中弃用,请避免使用。

// 替代, +[NSLayoutConstraint deactivateConstraints:].

- (void)removeConstraints:(NSArray<__kindof NSLayoutConstraint *> *)constraints NS_AVAILABLE_IOS(6_0); 

@end

// Core Layout Methods  (核心布局方法)

/* To render a window, the following passes will occur, if necessary.

   如果必要,呈现一个 window 后,下面的事项将会发生。

 // 更新约束

 update constraints

 // 布局

 layout

 // 显示

 display

 Please see the conceptual documentation for a discussion of these methods.

 请参见概念性文档的讨论这些方法。

 */

@interface UIView (UIConstraintBasedLayoutCoreMethods)

// Updates the constraints from the bottom up for the view hierarchy rooted at the receiver. UIWindow's implementation creates a layout engine if necessary first.

// 从视图层级底部更新到上部分,UIWindow 在必要的情况下回创建约束引擎

/*

    子类不会重载这个方法。

    每当一个 view 的新的布局被触发的时候,系统将会调用这个方法来确保view 和subview的根据当前视图的层级和约束信息来更新任何约束。这个方法会被系统自动调用,也可能被手动触发

,如果你需要检查最新的约束。

*/ 

- (void)updateConstraintsIfNeeded NS_AVAILABLE_IOS(6_0); 

// Override this to adjust your special constraints during a constraints update pass

// 重载这个约束去调整你指定的约束,在约束的调整过程中。

/*

   调用 [super updateConstraints] 作为实现的最后一步。

*/

- (void)updateConstraints NS_AVAILABLE_IOS(6_0); 

// 一个布尔值来决定是否需要更新视图的约束。

- (BOOL)needsUpdateConstraints NS_AVAILABLE_IOS(6_0);

// 控件视图的约束是否需要更新。

/*

    当自定义的视图的属性发生改变的时候,这个改变将会影响约束。你能够调用这个方法。去表明在将来的某个时间点约束需要更新。 系统之后将会调用 updateConstraints 作为正常布局的一部分。

*/

- (void)setNeedsUpdateConstraints NS_AVAILABLE_IOS(6_0);

@end

// Compatibility and Adoption

@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

// Separation of Concerns

@interface UIView (UIConstraintBasedLayoutLayering)

/* Constraints do not actually relate the frames of the views, rather they relate the "alignment rects" of views. This is the same as the frame unless overridden by a subclass of UIView. Alignment rects are the same as the "layout rects" shown in Interface Builder 3. Typically the alignment rect of a view is what the end user would think of as the bounding rect around a control, omitting ornamentation like shadows and engraving lines. The edges of the alignment rect are what is interesting to align, not the shadows and such.

 */

/* These two methods should be inverses of each other. UIKit will call both as part of layout computation.

 They may be overridden to provide arbitrary transforms between frame and alignment rect, though the two methods must be inverses of each other.

 However, the default implementation uses -alignmentRectInsets, so just override that if it's applicable. It's easier to get right.

 A view that displayed an image with some ornament would typically override these, because the ornamental part of an image would scale up with the size of the frame.

 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);

- (UIView *)viewForBaselineLayout NS_DEPRECATED_IOS(6_0, 9_0, "Override -viewForFirstBaselineLayout or -viewForLastBaselineLayout as appropriate, instead") __TVOS_PROHIBITED;

/* -viewForFirstBaselineLayout is called by the constraints system when interpreting

 the firstBaseline attribute for a view.

 For complex custom UIView subclasses, override this method to return the text-based

 (i.e., UILabel or non-scrollable UITextView) descendant of the receiver whose first baseline

 is appropriate for alignment.

 UIView's implementation returns [self viewForLastBaselineLayout], so if the same

 descendant is appropriate for both first- and last-baseline layout you may override

 just -viewForLastBaselineLayout.

 */

@property(readonly,strong) UIView *viewForFirstBaselineLayout NS_AVAILABLE_IOS(9_0);

/* -viewForLastBaselineLayout is called by the constraints system when interpreting

 the lastBaseline attribute for a view.

 For complex custom UIView subclasses, override this method to return the text-based

 (i.e., UILabel or non-scrollable UITextView) descendant of the receiver whose last baseline

 is appropriate for alignment.

 UIView's implementation returns self.

 */

@property(readonly,strong) UIView *viewForLastBaselineLayout NS_AVAILABLE_IOS(9_0);

/* Override this method to tell the layout system that there is something it doesn't natively understand in this view, and this is how large it intrinsically is. A typical example would be a single line text field. The layout system does not understand text - it must just be told that there's something in the view, and that that something will take a certain amount of space if not clipped.

 In response, UIKit will set up constraints that specify (1) that the opaque content should not be compressed or clipped, (2) that the view prefers to hug tightly to its content.

 A user of a view may need to specify the priority of these constraints. For example, by default, a push button

 -strongly wants to hug its content in the vertical direction (buttons really ought to be their natural height)

 -weakly hugs its content horizontally (extra side padding between the title and the edge of the bezel is acceptable)

 -strongly resists compressing or clipping content in both directions.

 However, you might have a case where you'd prefer to show all the available buttons with truncated text rather than losing some of the buttons. The truncation might only happen in portrait orientation but not in landscape, for example. In that case you'd want to setContentCompressionResistancePriority:forAxis: to (say) UILayoutPriorityDefaultLow for the horizontal axis.

 The default 'strong' and 'weak' priorities referred to above are UILayoutPriorityDefaultHigh and UILayoutPriorityDefaultLow.

 Note that not all views have an intrinsicContentSize. UIView's default implementation is to return (UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric). The _intrinsic_ content size is concerned only with data that is in the view itself, not in other views. 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.

 */

UIKIT_EXTERN const CGFloat UIViewNoIntrinsicMetric NS_AVAILABLE_IOS(6_0); // -1

- (CGSize)intrinsicContentSize NS_AVAILABLE_IOS(6_0);

- (void)invalidateIntrinsicContentSize NS_AVAILABLE_IOS(6_0); // call this when something changes that affects the intrinsicContentSize. Otherwise UIKit won't notice that it changed.

- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

@end

// Size To Fit  (尺寸适应)

// 尺寸适应的压缩尺寸

UIKIT_EXTERN const CGSize UILayoutFittingCompressedSize NS_AVAILABLE_IOS(6_0);

// 尺寸适应的扩充尺寸

UIKIT_EXTERN const CGSize UILayoutFittingExpandedSize NS_AVAILABLE_IOS(6_0);

@interface UIView (UIConstraintBasedLayoutFittingSize)

/* The size fitting most closely to targetSize in which the receiver's subtree can be laid out while optimally satisfying the constraints. If you want the smallest possible size, pass UILayoutFittingCompressedSize; for the largest possible size, pass UILayoutFittingExpandedSize.

 Also see the comment for UILayoutPriorityFittingSizeLevel.

 */

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize NS_AVAILABLE_IOS(6_0); 

// Equivalent to sending -systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority: with UILayoutPriorityFittingSizeLevel for both priorities.

- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority NS_AVAILABLE_IOS(8_0);

@end

@interface UIView (UILayoutGuideSupport)

/* UILayoutGuide objects owned by the receiver.

 */

@property(nonatomic,readonly,copy) NSArray<__kindof UILayoutGuide *> *layoutGuides NS_AVAILABLE_IOS(9_0);

/* Adds layoutGuide to the receiver, passing the receiver in -setOwningView: to layoutGuide.

 */

- (void)addLayoutGuide:(UILayoutGuide *)layoutGuide NS_AVAILABLE_IOS(9_0);

/* Removes layoutGuide from the receiver, passing nil in -setOwningView: to layoutGuide.

 */

- (void)removeLayoutGuide:(UILayoutGuide *)layoutGuide NS_AVAILABLE_IOS(9_0);

@end

@class NSLayoutXAxisAnchor,NSLayoutYAxisAnchor,NSLayoutDimension;

@interface UIView (UIViewLayoutConstraintCreation)

/* Constraint creation conveniences. See NSLayoutAnchor.h for details.

 */

@property(readonly, strong) NSLayoutXAxisAnchor *leadingAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutXAxisAnchor *trailingAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutXAxisAnchor *leftAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutXAxisAnchor *rightAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutYAxisAnchor *topAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutYAxisAnchor *bottomAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutDimension *widthAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutDimension *heightAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutXAxisAnchor *centerXAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutYAxisAnchor *centerYAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutYAxisAnchor *firstBaselineAnchor NS_AVAILABLE_IOS(9_0);

@property(readonly, strong) NSLayoutYAxisAnchor *lastBaselineAnchor NS_AVAILABLE_IOS(9_0);

@end

// Debugging

/* Everything in this section should be used in debugging only, never in shipping code. These methods may not exist in the future - no promises.

 */

@interface UIView (UIConstraintBasedLayoutDebugging)

/* This returns a list of all the constraints that are affecting the current location of the receiver. The constraints do not necessarily involve the receiver, they may affect the frame indirectly.

 Pass UILayoutConstraintAxisHorizontal for the constraints affecting [self center].x and CGRectGetWidth([self bounds]), and UILayoutConstraintAxisVertical for the constraints affecting[self center].y and CGRectGetHeight([self bounds]).

 */

- (NSArray<__kindof NSLayoutConstraint *> *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis NS_AVAILABLE_IOS(6_0);

/* If there aren't enough constraints in the system to uniquely determine layout, we say the layout is ambiguous. For example, if the only constraint in the system was x = y + 100, then there are lots of different possible values for x and y. This situation is not automatically detected by UIKit, due to performance considerations and details of the algorithm used for layout.

 The symptom of ambiguity is that views sometimes jump from place to place, or possibly are just in the wrong place.

 -hasAmbiguousLayout runs a check for whether there is another center and bounds the receiver could have that could also satisfy the constraints.

 -exerciseAmbiguousLayout does more. It randomly changes the view layout to a different valid layout. Making the UI jump back and forth can be helpful for figuring out where you're missing a constraint.

 */

- (BOOL)hasAmbiguousLayout NS_AVAILABLE_IOS(6_0);

- (void)exerciseAmbiguityInLayout NS_AVAILABLE_IOS(6_0);

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,921评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,635评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,393评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,836评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,833评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,685评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,043评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,694评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,671评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,670评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,779评论 1 332
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,424评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,027评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,984评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,214评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,108评论 2 351
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,517评论 2 343

推荐阅读更多精彩内容