//
// NSLayoutConstraint.h
// UIKit
//
// Copyright (c) 2009-2015 Apple Inc. All rights reserved.
//



#import <Foundation/NSObject.h>
#import <UIKit/UIGeometry.h>



NS_ASSUME_NONNULL_BEGIN

@class NSArray, NSDictionary;


// 约束的关系
typedef NS_ENUM(NSInteger, NSLayoutRelation) {
     // 小于等于
     NSLayoutRelationLessThanOrEqual = -1,
     // 等于
     NSLayoutRelationEqual = 0,
     // 大于等于
     NSLayoutRelationGreaterThanOrEqual = 1,
};


// 布局属性
typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
     // 左边
     NSLayoutAttributeLeft = 1,

     // 右边
     NSLayoutAttributeRight,

     // 上面
     NSLayoutAttributeTop,

     // 下面
     NSLayoutAttributeBottom,

     // 前面
     NSLayoutAttributeLeading,

     // 右面
     NSLayoutAttributeTrailing,

     // 宽度
     NSLayoutAttributeWidth,

     // 高度
     NSLayoutAttributeHeight,

     // 中心x
     NSLayoutAttributeCenterX,

     // 中心y
     NSLayoutAttributeCenterY,

     // 基准线
     NSLayoutAttributeBaseline,

     // 底部基准线
     NSLayoutAttributeLastBaseline = NSLayoutAttributeBaseline,

     // 顶部基准线
     NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),




     // 间隙
     // 左边间隙
     NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),

     // 右边间隙
     NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),

     // 顶部间隙
     NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),

     // 底部间隙
     NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),

     // 前面间隙
     NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),

     // 右面间隙
     NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),

     // 中心 x 轴的间隙
     NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),

     // 中心 y 轴的间隙
     NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),


     // 没有约束(一般设置在 宽度和高度 )
     NSLayoutAttributeNotAnAttribute = 0

};


// 布局格式枚举
typedef NS_OPTIONS(NSUInteger, NSLayoutFormatOptions) {


     NSLayoutFormatAlignAllLeft = (1 << NSLayoutAttributeLeft),

     NSLayoutFormatAlignAllRight = (1 << NSLayoutAttributeRight),

     NSLayoutFormatAlignAllTop = (1 << NSLayoutAttributeTop),

     NSLayoutFormatAlignAllBottom = (1 << NSLayoutAttributeBottom),

     NSLayoutFormatAlignAllLeading = (1 << NSLayoutAttributeLeading),

     NSLayoutFormatAlignAllTrailing = (1 << NSLayoutAttributeTrailing),

     NSLayoutFormatAlignAllCenterX = (1 << NSLayoutAttributeCenterX),

     NSLayoutFormatAlignAllCenterY = (1 << NSLayoutAttributeCenterY),

     NSLayoutFormatAlignAllBaseline = (1 << NSLayoutAttributeBaseline),

     NSLayoutFormatAlignAllLastBaseline = NSLayoutFormatAlignAllBaseline,

     NSLayoutFormatAlignAllFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0) = (1 << NSLayoutAttributeFirstBaseline),



     NSLayoutFormatAlignmentMask = 0xFFFF,



     /* choose only one of these three

     */

     NSLayoutFormatDirectionLeadingToTrailing = 0 << 16, // default

     NSLayoutFormatDirectionLeftToRight = 1 << 16,

     NSLayoutFormatDirectionRightToLeft = 2 << 16,



     NSLayoutFormatDirectionMask = 0x3 << 16,

};


// 约束优先级
typedef float UILayoutPriority;

// 必须的优先级 1000
// A required constraint. Do not exceed this.
// 一个必须的约束,不能超过
static const UILayoutPriority UILayoutPriorityRequired NS_AVAILABLE_IOS(6_0) = 1000; 


// 默认的约束
// 搞优先级
// This is the priority level with which a button resists compressing its content.
// 这是一个 btn 抗拒内容压缩的优先级。
static const UILayoutPriority UILayoutPriorityDefaultHigh NS_AVAILABLE_IOS(6_0) = 750; 


// 低优先级
// This is the priority level at which a button hugs its contents horizontally.
// 这是一个按钮的优先级水平拥抱它的内容。
static const UILayoutPriority UILayoutPriorityDefaultLow NS_AVAILABLE_IOS(6_0) = 250; 



// When you send -[UIView systemLayoutSizeFittingSize:], the size fitting most closely to the target size (the argument) is computed. 
    UILayoutPriorityFittingSizeLevel is the priority level with which the view wants to conform to the target size in that computation. 
    It's quite low. It is generally not appropriate to make a constraint at exactly this priority. 
    You want to be higher or lower.
// 适应尺寸的优先级
static const UILayoutPriority UILayoutPriorityFittingSizeLevel NS_AVAILABLE_IOS(6_0) = 50; 







NS_CLASS_AVAILABLE_IOS(6_0)

@interface NSLayoutConstraint : NSObject



/* Create an array of constraints using an ASCII art-like visual format string.
   使用 vf 字符串创建一个约束数组 
 */

+ (NSArray<__kindof NSLayoutConstraint *> *)constraintsWithVisualFormat:(NSString *)format 
                                                                options:(NSLayoutFormatOptions)opts 
                                                                metrics:(nullable NSDictionary<NSString *,id> *)metrics     
                                                                  views:(NSDictionary<NSString *, id> *)views;



/* This macro is a helper for making view dictionaries for +constraintsWithVisualFormat:options:metrics:views:.

 NSDictionaryOfVariableBindings(v1, v2, v3) is equivalent to [NSDictionary dictionaryWithObjectsAndKeys:v1, @"v1", v2, @"v2", v3, @"v3", nil];

 */

#define NSDictionaryOfVariableBindings(...) _NSDictionaryOfVariableBindings(@"" # __VA_ARGS__, __VA_ARGS__, nil)

UIKIT_EXTERN NSDictionary *_NSDictionaryOfVariableBindings(NSString *commaSeparatedKeysString, __nullable id firstValue, ...) NS_AVAILABLE_IOS(6_0); // not for direct use





/* Create constraints explicitly.
   创建一个明确的约束

   Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant"
   约束就像这样的格式

   If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.
   如果你的方程中没有第二个view 或者 属性,使用 nil 和  NSLayoutAttributeNotAnAttribute 替代。
 */

+(instancetype)constraintWithItem:(id)view1 
                        attribute:(NSLayoutAttribute)attr1 
                        relatedBy:(NSLayoutRelation)relation 
                           toItem:(nullable id)view2 
                        attribute:(NSLayoutAttribute)attr2 
                       multiplier:(CGFloat)multiplier 
                         constant:(CGFloat)c;



/* If a constraint's priority level is less than UILayoutPriorityRequired, then it is optional. Higher priority constraints are met before lower priority constraints.

 Constraint satisfaction is not all or nothing. If a constraint 'a == b' is optional, that means we will attempt to minimize 'abs(a-b)'.

 This property may only be modified as part of initial set up. An exception will be thrown if it is set after a constraint has been added to a view.

 */

@property UILayoutPriority priority;



/* When a view is archived, it archives some but not all constraints in its -constraints array. The value of shouldBeArchived informs UIView if a particular constraint should be archived by UIView.

 If a constraint is created at runtime in response to the state of the object, it isn't appropriate to archive the constraint - rather you archive the state that gives rise to the constraint. Since the majority of constraints that should be archived are created in Interface Builder (which is smart enough to set this prop to YES), the default value for this property is NO.

 */
// 将要被归档
@property BOOL shouldBeArchived;



/* accessors (存储器)

 firstItem.firstAttribute {==,<=,>=} secondItem.secondAttribute * multiplier + constant

 */
// 这里主要是通过公式来设置的几个属性( 都是只读的属性 )
// 第一个视图
@property (readonly, assign) id firstItem;

// 第一个布局属性
@property (readonly) NSLayoutAttribute firstAttribute;

// 关系(大于等于,等于,小于等于)
@property (readonly) NSLayoutRelation relation;

// 第二个视图
@property (nullable, readonly, assign) id secondItem;

// 第二个布局属性
@property (readonly) NSLayoutAttribute secondAttribute;

// 倍数
@property (readonly) CGFloat multiplier;

/* Unlike the other properties, the constant may be modified after constraint creation.
 Setting the constant on an existing constraint performs much better than removing the constraint and adding a new one that's just like the old but for having a new constant.
  不像另外一些属性一样,这个常量是可以在约束被创建后被修改的。
  使用一个新的常数去修改以及存在的约束比移除一个约束创建和就约束一样的约束更加的高效。
 */
// 常量
@property CGFloat constant;










/* The receiver may be activated or deactivated by manipulating this property. 
   约束能够通过这个属性设置激活和失效。
   Only active constraints affect the calculated layout. 
   只有激活的约束才可以计算布局
   Attempting to activate a constraint whose items have no common ancestor will cause an exception to be thrown. 
   试着去激活一个没有共同父类视图的将要导致一个异常
   Defaults to NO for newly created constraints. 
   一个新的约束默认是没有激活的
*/
// 激活约束
@property (getter=isActive) BOOL active NS_AVAILABLE(10_10, 8_0);



/* Convenience method that activates each constraint in the contained array, in the same manner as setting active=YES. This is often more efficient than activating each constraint individually. 

  便利方法,去激活容器数组中的每一个约束。以同样的方式设置活动= YES。这通常是更有效的比单独激活每个约束。
*/
// 激活约束
+ (void)activateConstraints:(NSArray<NSLayoutConstraint *> *)constraints NS_AVAILABLE(10_10, 8_0);



/* Convenience method that deactivates each constraint in the contained array, in the same manner as setting active=NO. This is often more efficient than deactivating each constraint individually. 

  便利方法,去失效容器数组中每一个约束。以同样的方式设置活动= YES。这通常是更有效的比单独激活每个约束。
*/

+ (void)deactivateConstraints:(NSArray<NSLayoutConstraint *> *)constraints NS_AVAILABLE(10_10, 8_0);

@end


// 约束的标识分类
@interface NSLayoutConstraint (NSIdentifier)

/* For ease in debugging, name a constraint by setting its identifier, which will be printed in the constraint's description.
  在 bebugg 的时候就会很简单。 通过这个标识给约束设置名称。将会用过约束的描述打印出来。

 Identifiers starting with UI and NS are reserved by the system.
 ns 和 ui 开头的约束是系统预留的。
 */
@property (nullable, copy) NSString *identifier NS_AVAILABLE_IOS(7_0);

@end










/*

 UILayoutSupport protocol is implemented by layout guide objects
 returned by UIViewController properties topLayoutGuide and bottomLayoutGuide.

 约束协议通过 layout guide 对象来实现。通过 UIViewController 的 topLayoutGuide 和 bottomLayoutGuide。


 These guide objects may be used as layout items in the NSLayoutConstraint

 factory methods.

 */

@class NSLayoutYAxisAnchor, NSLayoutDimension;


// 布局协议
@protocol UILayoutSupport <NSObject>

// As a courtesy when not using auto layout, this value is safe to refer to in -viewDidLayoutSubviews, or in -layoutSubviews after calling super
@property(nonatomic,readonly) CGFloat length; 


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

 */
// 顶部锚点
@property(readonly, strong) NSLayoutYAxisAnchor *topAnchor NS_AVAILABLE_IOS(9_0);
// 底部锚点
@property(readonly, strong) NSLayoutYAxisAnchor *bottomAnchor NS_AVAILABLE_IOS(9_0);
// 高度锚点
@property(readonly, strong) NSLayoutDimension *heightAnchor NS_AVAILABLE_IOS(9_0);

@end



NS_ASSUME_NONNULL_END

results matching ""

    No results matching ""