• iOS Masonry 使用小记(MASConstraint、center、centerX、edges、equalTo、greaterThanOrEqualTo)


    项目中我们经常使用Masonry布局,现在简单记录下自己常用的几种布局方式

    1、center 垂直水平居中

    [self.bgView addSubview:self.tagLabel];
        [self.tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.bgView);
        }];

    2、水平居中 centerY

         垂直居中 centerX

    [self.bgView addSubview:self.tagLabel];
        [self.tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.bgView);
            make.centerY.equalTo(self.bgView);
        }];

    3、设置四周间距(和父控件相等大小就是间距为0)

        A.使用edges设置

        和父控件相等大小

    [self.contentView addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];

       上下10 左右15

    UIEdgeInsets
    UIKIT_STATIC_INLINE UIEdgeInsets UIEdgeInsetsMake(CGFloat top, CGFloat left, CGFloat bottom, CGFloat right) {
        UIEdgeInsets insets = {top, left, bottom, right};
        return insets;
    }
    [self.contentView addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(10, 15, 10, 15));
        }];

       B.使用top、left、bottom、right设置

          和父控件相等大小

    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView);
            make.top.equalTo(self.contentView);
        }];
    [self.contentView addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(self.contentView);
        }];

          上下10 左右15

    [self.contentView addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(15);
            make.right.equalTo(self.contentView).offset(-15);
            make.bottom.equalTo(self.contentView).offset(-10);
            make.top.equalTo(self.contentView).offset(10);
        }];

    备注:

    相关API

    /**
     *  Provides factory methods for creating MASConstraints.
     *  Constraints are collected until they are ready to be installed
     *
     */
    @interface MASConstraintMaker : NSObject
    
    /**
     *    The following properties return a new MASViewConstraint
     *  with the first item set to the makers associated view and the appropriate MASViewAttribute
     */
    @property (nonatomic, strong, readonly) MASConstraint *left;
    @property (nonatomic, strong, readonly) MASConstraint *top;
    @property (nonatomic, strong, readonly) MASConstraint *right;
    @property (nonatomic, strong, readonly) MASConstraint *bottom;
    @property (nonatomic, strong, readonly) MASConstraint *leading;
    @property (nonatomic, strong, readonly) MASConstraint *trailing;
    @property (nonatomic, strong, readonly) MASConstraint *width;
    @property (nonatomic, strong, readonly) MASConstraint *height;
    @property (nonatomic, strong, readonly) MASConstraint *centerX;
    @property (nonatomic, strong, readonly) MASConstraint *centerY;
    @property (nonatomic, strong, readonly) MASConstraint *baseline;
    
    #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
    
    @property (nonatomic, strong, readonly) MASConstraint *firstBaseline;
    @property (nonatomic, strong, readonly) MASConstraint *lastBaseline;
    
    #endif
    
    #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000)
    
    @property (nonatomic, strong, readonly) MASConstraint *leftMargin;
    @property (nonatomic, strong, readonly) MASConstraint *rightMargin;
    @property (nonatomic, strong, readonly) MASConstraint *topMargin;
    @property (nonatomic, strong, readonly) MASConstraint *bottomMargin;
    @property (nonatomic, strong, readonly) MASConstraint *leadingMargin;
    @property (nonatomic, strong, readonly) MASConstraint *trailingMargin;
    @property (nonatomic, strong, readonly) MASConstraint *centerXWithinMargins;
    @property (nonatomic, strong, readonly) MASConstraint *centerYWithinMargins;
    
    #endif
    
    /**
     *  Returns a block which creates a new MASCompositeConstraint with the first item set
     *  to the makers associated view and children corresponding to the set bits in the
     *  MASAttribute parameter. Combine multiple attributes via binary-or.
     */
    @property (nonatomic, strong, readonly) MASConstraint *(^attributes)(MASAttribute attrs);
    
    /**
     *    Creates a MASCompositeConstraint with type MASCompositeConstraintTypeEdges
     *  which generates the appropriate MASViewConstraint children (top, left, bottom, right)
     *  with the first item set to the makers associated view
     */
    @property (nonatomic, strong, readonly) MASConstraint *edges;
    
    /**
     *    Creates a MASCompositeConstraint with type MASCompositeConstraintTypeSize
     *  which generates the appropriate MASViewConstraint children (width, height)
     *  with the first item set to the makers associated view
     */
    @property (nonatomic, strong, readonly) MASConstraint *size;
    
    /**
     *    Creates a MASCompositeConstraint with type MASCompositeConstraintTypeCenter
     *  which generates the appropriate MASViewConstraint children (centerX, centerY)
     *  with the first item set to the makers associated view
     */
    @property (nonatomic, strong, readonly) MASConstraint *center;
    
    /**
     *  Whether or not to check for an existing constraint instead of adding constraint
     */
    @property (nonatomic, assign) BOOL updateExisting;
    
    /**
     *  Whether or not to remove existing constraints prior to installing
     */
    @property (nonatomic, assign) BOOL removeExisting;

    lessThanOrEqualTo、greaterThanOrEqualTo、equalTo

    @interface MASConstraint : NSObject
    
    // Chaining Support
    
    /**
     *    Modifies the NSLayoutConstraint constant,
     *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
     *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
     */
    - (MASConstraint * (^)(MASEdgeInsets insets))insets;
    
    /**
     *    Modifies the NSLayoutConstraint constant,
     *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
     *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
     */
    - (MASConstraint * (^)(CGFloat inset))inset;
    
    /**
     *    Modifies the NSLayoutConstraint constant,
     *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
     *  NSLayoutAttributeWidth, NSLayoutAttributeHeight
     */
    - (MASConstraint * (^)(CGSize offset))sizeOffset;
    
    /**
     *    Modifies the NSLayoutConstraint constant,
     *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
     *  NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
     */
    - (MASConstraint * (^)(CGPoint offset))centerOffset;
    
    /**
     *    Modifies the NSLayoutConstraint constant
     */
    - (MASConstraint * (^)(CGFloat offset))offset;
    
    /**
     *  Modifies the NSLayoutConstraint constant based on a value type
     */
    - (MASConstraint * (^)(NSValue *value))valueOffset;
    
    /**
     *    Sets the NSLayoutConstraint multiplier property
     */
    - (MASConstraint * (^)(CGFloat multiplier))multipliedBy;
    
    /**
     *    Sets the NSLayoutConstraint multiplier to 1.0/dividedBy
     */
    - (MASConstraint * (^)(CGFloat divider))dividedBy;
    
    /**
     *    Sets the NSLayoutConstraint priority to a float or MASLayoutPriority
     */
    - (MASConstraint * (^)(MASLayoutPriority priority))priority;
    
    /**
     *    Sets the NSLayoutConstraint priority to MASLayoutPriorityLow
     */
    - (MASConstraint * (^)(void))priorityLow;
    
    /**
     *    Sets the NSLayoutConstraint priority to MASLayoutPriorityMedium
     */
    - (MASConstraint * (^)(void))priorityMedium;
    
    /**
     *    Sets the NSLayoutConstraint priority to MASLayoutPriorityHigh
     */
    - (MASConstraint * (^)(void))priorityHigh;
    
    /**
     *    Sets the constraint relation to NSLayoutRelationEqual
     *  returns a block which accepts one of the following:
     *    MASViewAttribute, UIView, NSValue, NSArray
     *  see readme for more details.
     */
    - (MASConstraint * (^)(id attr))equalTo;
    
    /**
     *    Sets the constraint relation to NSLayoutRelationGreaterThanOrEqual
     *  returns a block which accepts one of the following:
     *    MASViewAttribute, UIView, NSValue, NSArray
     *  see readme for more details.
     */
    - (MASConstraint * (^)(id attr))greaterThanOrEqualTo;
    
    /**
     *    Sets the constraint relation to NSLayoutRelationLessThanOrEqual
     *  returns a block which accepts one of the following:
     *    MASViewAttribute, UIView, NSValue, NSArray
     *  see readme for more details.
     */
    - (MASConstraint * (^)(id attr))lessThanOrEqualTo;
    
    /**
     *    Optional semantic property which has no effect but improves the readability of constraint
     */
    - (MASConstraint *)with;
    
    /**
     *    Optional semantic property which has no effect but improves the readability of constraint
     */
    - (MASConstraint *)and;
    
    /**
     *    Creates a new MASCompositeConstraint with the called attribute and reciever
     */
    - (MASConstraint *)left;
    - (MASConstraint *)top;
    - (MASConstraint *)right;
    - (MASConstraint *)bottom;
    - (MASConstraint *)leading;
    - (MASConstraint *)trailing;
    - (MASConstraint *)width;
    - (MASConstraint *)height;
    - (MASConstraint *)centerX;
    - (MASConstraint *)centerY;
    - (MASConstraint *)baseline;
    
    #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000) || (__MAC_OS_X_VERSION_MIN_REQUIRED >= 101100)
    
    - (MASConstraint *)firstBaseline;
    - (MASConstraint *)lastBaseline;
    
    #endif
    
    #if (__IPHONE_OS_VERSION_MIN_REQUIRED >= 80000) || (__TV_OS_VERSION_MIN_REQUIRED >= 9000)
    
    - (MASConstraint *)leftMargin;
    - (MASConstraint *)rightMargin;
    - (MASConstraint *)topMargin;
    - (MASConstraint *)bottomMargin;
    - (MASConstraint *)leadingMargin;
    - (MASConstraint *)trailingMargin;
    - (MASConstraint *)centerXWithinMargins;
    - (MASConstraint *)centerYWithinMargins;
    
    #endif
    
    
    /**
     *    Sets the constraint debug name
     */
    - (MASConstraint * (^)(id key))key;
    
    // NSLayoutConstraint constant Setters
    // for use outside of mas_updateConstraints/mas_makeConstraints blocks
    
    /**
     *    Modifies the NSLayoutConstraint constant,
     *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
     *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
     */
    - (void)setInsets:(MASEdgeInsets)insets;
    
    /**
     *    Modifies the NSLayoutConstraint constant,
     *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
     *  NSLayoutAttributeTop, NSLayoutAttributeLeft, NSLayoutAttributeBottom, NSLayoutAttributeRight
     */
    - (void)setInset:(CGFloat)inset;
    
    /**
     *    Modifies the NSLayoutConstraint constant,
     *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
     *  NSLayoutAttributeWidth, NSLayoutAttributeHeight
     */
    - (void)setSizeOffset:(CGSize)sizeOffset;
    
    /**
     *    Modifies the NSLayoutConstraint constant,
     *  only affects MASConstraints in which the first item's NSLayoutAttribute is one of the following
     *  NSLayoutAttributeCenterX, NSLayoutAttributeCenterY
     */
    - (void)setCenterOffset:(CGPoint)centerOffset;
    
    /**
     *    Modifies the NSLayoutConstraint constant
     */
    - (void)setOffset:(CGFloat)offset;
    
    
    // NSLayoutConstraint Installation support
    
    #if TARGET_OS_MAC && !(TARGET_OS_IPHONE || TARGET_OS_TV)
    /**
     *  Whether or not to go through the animator proxy when modifying the constraint
     */
    @property (nonatomic, copy, readonly) MASConstraint *animator;
    #endif
    
    /**
     *  Activates an NSLayoutConstraint if it's supported by an OS. 
     *  Invokes install otherwise.
     */
    - (void)activate;
    
    /**
     *  Deactivates previously installed/activated NSLayoutConstraint.
     */
    - (void)deactivate;
    
    /**
     *    Creates a NSLayoutConstraint and adds it to the appropriate view.
     */
    - (void)install;
    
    /**
     *    Removes previously installed NSLayoutConstraint
     */
    - (void)uninstall;
    
    @end
    
    
    /**
     *  Convenience auto-boxing macros for MASConstraint methods.
     *
     *  Defining MAS_SHORTHAND_GLOBALS will turn on auto-boxing for default syntax.
     *  A potential drawback of this is that the unprefixed macros will appear in global scope.
     */
    #define mas_equalTo(...)                 equalTo(MASBoxValue((__VA_ARGS__)))
    #define mas_greaterThanOrEqualTo(...)    greaterThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
    #define mas_lessThanOrEqualTo(...)       lessThanOrEqualTo(MASBoxValue((__VA_ARGS__)))
    
    #define mas_offset(...)                  valueOffset(MASBoxValue((__VA_ARGS__)))
    
    
    #ifdef MAS_SHORTHAND_GLOBALS
    
    #define equalTo(...)                     mas_equalTo(__VA_ARGS__)
    #define greaterThanOrEqualTo(...)        mas_greaterThanOrEqualTo(__VA_ARGS__)
    #define lessThanOrEqualTo(...)           mas_lessThanOrEqualTo(__VA_ARGS__)
    
    #define offset(...)                      mas_offset(__VA_ARGS__)
    
    #endif
    
    
    @interface MASConstraint (AutoboxingSupport)
    
    /**
     *  Aliases to corresponding relation methods (for shorthand macros)
     *  Also needed to aid autocompletion
     */
    - (MASConstraint * (^)(id attr))mas_equalTo;
    - (MASConstraint * (^)(id attr))mas_greaterThanOrEqualTo;
    - (MASConstraint * (^)(id attr))mas_lessThanOrEqualTo;
    
    /**
     *  A dummy method to aid autocompletion
     */
    - (MASConstraint * (^)(id offset))mas_offset;
    
    @end
  • 相关阅读:
    MyBatis 学习笔记
    JavaEE路径陷阱之getRealPath
    Java路径问题最终解决方案—可定位所有资源的相对路径寻址
    Hibernate4.3.10通过slf4j使用log4j
    Hibernate关联关系映射
    SpringMVC 学习笔记
    Spring 学习笔记
    Hibernate 学习笔记
    Struts2 学习笔记
    vue element tree组件,根据不同的状态显示不同的字体颜色
  • 原文地址:https://www.cnblogs.com/lijianyi/p/15007010.html
Copyright © 2020-2023  润新知