Masonry of common iOS libraries

Source: Internet
Author: User

Simple Introduction

Masonry Source Address: Https://github.com/Masonry/Masonry
Masonry is a lightweight layout framework that has its own description syntax with a more elegant chained syntax encapsulation automatic layout is straightforward and highly readable and supports both IOS and MAC OS x.

```
Pod ' masonry '
```

Use masonry to import header file ' #import ' Masonry.h "'

System API vs Masonry

System API

Nslayoutconstraint

"' Objective-c

Nslayoutconstraint *topconstraint = [Nslayoutconstraint constraintwithitem:item attribute:nslayoutattributetop Relatedby:nslayoutrelationequal toitem:item2 Attribute:nslayoutattributebottom Multiplier:1.0 constant: 0 ]; [Self.view Addsubview:topconstraint];

```

Masonry API

First look at an official sample code to meet masonry

```

[View1 mas_makeconstraints:^ (Masconstraintmaker * make) {    make.edges.equalTo (superview). With.insets ( padding);}];

'

See the word in block: ' Make Edgesequalto superview with insets '
give view1 to AutoLayout through a chained natural language

Is it easy to understand

  introduction

    Action Constraint method

    masonry processing constraint is mostly ' view+masadditions ', which is ' UIView ' of ' category '

' objective-c

// add constraint -(Nsarray *) mas_makeconstraints: (void(^) (Masconstraintmaker * Make)) block; //  /// can only modify the value of the constraint, you cannot change the constraint relationship, you cannot change the two item and two property in the core Formula-(Nsarray *) Mas_updateconstraints: ( void (^) (Masconstraintmaker * Make)) block; // Reset constraint Remove all existing constraints add again -(Nsarray *) mas_remakeconstraints: (void(^) (Masconstraintmaker *make)) Block


```

Masconstraintmaker Property

The ' masconstraint ' type attribute of the ' masconstraintmaker ' constraint object in masonry:


"' Objective-c

@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, @property (nonatomic, Strong,ReadOnly) Masconstraint *edges, @property (nonatomic, Strong,ReadOnly) Masconstraint *size; @property (nonatomic, Strong,ReadOnly) Masconstraint *center;

```


Where leading and left trailing are normally equivalent to right but when some of the layouts are in the correct-to-ieft (such as in Arabic? No similar experience) will be reversed in other words, it's basically possible to ignore left and right.

Relation

There are several methods in the ' masconstraint ' category that represent the relationship between view and view

"' Objective-c
-(Masconstraint * (^) (id attr)) mas_equalto;
-(Masconstraint * (^) (id attr)) mas_greaterthanorequalto;
-(Masconstraint * (^) (id attr)) mas_lessthanorequalto;
```

The call to the method related to the relation method above will return the Masconstraint object

Extended properties of UIView

"' Objective-c

@property (Nonatomic, Strong,ReadOnly) Masviewattribute *Mas_left, @property (nonatomic, Strong,ReadOnly) Masviewattribute *Mas_top, @property (nonatomic, Strong,ReadOnly) Masviewattribute *mas_right, @property (nonatomic, Strong,ReadOnly) Masviewattribute *Mas_bottom, @property (nonatomic, Strong,ReadOnly) Masviewattribute *mas_leading, @property (nonatomic, Strong,ReadOnly) Masviewattribute *mas_trailing, @property (nonatomic, Strong,ReadOnly) Masviewattribute *Mas_width, @property (nonatomic, Strong,ReadOnly) Masviewattribute *mas_height, @property (nonatomic, Strong,ReadOnly) Masviewattribute *Mas_centerx, @property (nonatomic, Strong,ReadOnly) Masviewattribute *Mas_centery, @property (nonatomic, Strong,ReadOnly) Masviewattribute *mas_baseline;


```

Masconstraint Related

"' Objective-c

//insets: Indent in four directions on top left and right-(Masconstraint * (^) (masedgeinsets insets)) insets;//Sizeoffset: Wide-height offset-(Masconstraint * (^) (cgsize offset)) Sizeoffset;//centeroffset: Center point offset-(Masconstraint * (^) (cgpoint offset)) Centeroffset;//Offset : Offsets-(Masconstraint * (^) (cgfloat offset)) offset;//offset in valueoffset:value form-(Masconstraint * (^) (Nsvalue *value)) Valueoffset;//Multipliedby: Adding multiplier in the constraint core formula-(Masconstraint * (^) (CGFloat multiplier)) Multipliedby;//priority: Setting priorities-(Masconstraint * (^) (maslayoutpriority priority));//Prioritylow: Low Priority-(Masconstraint * (^) ()) Prioritylow;//Prioritymedium: In priority-(Masconstraint * (^) ()) Prioritymedium;//Priorityhigh: High priority-(Masconstraint * (^) ()) Priorityhigh;//Masviewattribute, UIView, Nsvalue, Nsarray and other types that represent the constraint relationship , back to block-(Masconstraint * (^) (IDattr)) Equalto;-(Masconstraint * (^) (IDattr)) Greaterthanorequalto;-(Masconstraint * (^) (IDattr)) Lessthanorequalto;//Optional semantic attributes, no effect on code, just to improve code readability-(Masconstraint *) with;-(Masconstraint *) and;-(Masconstraint *) left;-(Masconstraint *) top;-(Masconstraint *) right;-(Masconstraint *) Bottom;-(Masconstraint *) leading;-(Masconstraint *) trailing;-(Masconstraint *) width;-(Masconstraint *) height;-(Masconstraint *) CenterX;-(Masconstraint *) CenterY;-(Masconstraint *) baseline;

```

Two simple macros

to increase the readability of the code there are two simplified code macros:
' #define Mas_shorthand ': As long as you define this macro before importing the masonry primary header file, you can omit the ' mas_ ' prefix later when using the properties and methods in the masonry framework
' #define Mas_shorthand_globals ': As long as you define this macro before importing the masonry primary header file, you can let the Equalto function receive the base data type, which wraps the underlying data type internally

NOTE * * *: These two macros must be defined before adding ' masonry ' header files if they want to be used effectively.

?

Masonry Precautions for use

1
. View must have been added to its parent view when adding a constraint using masonry;
2. Label to set the width of the line when changing lines
3. Animations remember to call layoutifneeded
4. Mas_makeconstraints is only responsible for new constraints. Masonry can not exist at the same time two for the same object constraint otherwise will error;
5. When using the three-mesh operator, be careful not to use the base data type
6. Mas_updateconstraints updates the value of the constraint that appears in the block and does not load the constraint relationship that did not exist before
7. IOS7 has two very useful properties, Toplayoutguide and Bottomlayoutguide, This two is primarily handy for getting the top of the Uinavigationcontroller and Uitabbarcontroller view area of the Head view area at the bottom of the top;


AutoLayout differences on several methods of updating

* Setneedsupdateconstraints: Tells the restraint system to update constraints, but does not start immediately
* updateconstraintsifneeded: Notify update constraint immediately
* layoutifneeded: Tell the page layout to be updated immediately. So the general will be used with setneedslayout. If you want to call this method to generate a new frame immediately, use this general layout animation to make the animation take effect directly after the layout is updated.
* Updateconstraints: The official recommended place to add, update constraint code, if you override this method, you need to call [Super updateconstraints] at the end of the method.
* Setneedslayout: Tells the page to be updated, but does not start the update immediately. Layoutsubviews is called immediately after execution.
* Layoutsubviews: System rewrite layout


Masonry of common iOS libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.