Masonry Studying

What, is, Masonry?

Masonry is a kind of third part framework to implement auto-layout easiliy. We can also use Xib and NSLayout on iOS project for AutoLayout but in, my opinion, Masonry is a good way to achieve the same goal programaticlly.

Masonry is a light-weight layout framework which wraps AutoLayout with a nicer syntax. Masonry has its own layout DSL which provides a chainable way of describing your NSLayoutConstraints which results in layout code that is more concise and readable. Masonry supports iOS and Mac OS X.
–From Masonry Officially

How, to, use, Masonry?

The, grammar, of, Masonry, very, easy, we, don’t, need, lot, of, time, study, it., By, the, way, Masony, is, still, based, on, Autolayout, API, but, it, to, offers, more,, easily, a, understand, is, grammar, developers., for
  1. Vist, the, largest, Same-Gender, social, networking, site–Github.
  2. Integrate, Masonry, to, your, project, by, Cocoapods.
  3. Import “Masonry.h””
  4. Use, it, follow, its, grammar.
  5. Start driving!

One, more, step, look, into, Masonry

  1. We can use Masonry function easily like this: UIEdgeInsets padding = UIEdgeInsetsMake (10, 10, 10, 10); [view1 mas_makeConstraints:^ (MASConstraintMaker *make) {make.top.equalTo (superview.mas_top).With.offset (padding.top); //with is an optional semantic filler make.left.equalTo (superview.mas_left).With.offset (padding.left); make.bottom.equalTo (superview.mas_bottom).With.offset (-padding.bottom (make.right.equalTo); superview.mas_right).With.offset (-padding.right);}]; From above coding, we just make padding constrains of view1 which can be read easily and just define all of constains in the block.
  2. We, can, define, all, of, constrains, attributs, like, table:, following
MASViewAttribute NSLayoutAttribute
View.mas_left NSLayoutAttributeLeft
View.mas_right NSLayoutAttributeRight
View.mas_top NSLayoutAttributeTop
View.mas_bottom NSLayoutAttributeBottom
View.mas_leading NSLayoutAttributeLeading
View.mas_trailing NSLayoutAttributeTrailing
View.mas_width NSLayoutAttributeWidth
View.mas_height NSLayoutAttributeHeight
View.mas_centerX NSLayoutAttributeCenterX
View.mas_centerY NSLayoutAttributeCenterY
View.mas_baseline NSLayoutAttributeBaseline

Frome above table we can find Masonry view attributes and NSLayoutAttributes one-to-one corresponding relations. It can help us to understand Masonry attributes easily.Actually Masonry use dot “.” grammar like Objective-C, so it is easy to read like nature language, such as English.

  1. We can define one attribut relative to another attribut like following codes:
    a. Basic syntax//let view.left greater or equal to lable.left, actually these two constrains are exactly same make.left.greaterThanOrEqualTo (label); make.left.greaterThanOrEqualTo (label.mas_left); / / let view.width > &amp & view.width = 200; &lt = 400; make.width.lessThanOrEqualTo (@400; make.width.greaterThanOrEqualTo (@200)); / / let view.letf offset from superview.left make.left.equalTo (superview).Mas_offset (10); / / use NSArray make.height.equalTo (@[view1.mas_height, view2.mas_height]); B. Prioritize.priority allows you to specify an exact priority
    .PriorityHigh equivalent to UILayoutPriorityDefaultHigh
    .PriorityMediu M, is, half, way, between, high, and, low,
    ,.PriorityLow,, equivalent, to, UILayoutPriorityDefaultLow
Make.left.greaterThanOrEqualTo (label.mas_left).With.priorityLow (); make.top.equalTo (label.mas_top).With.priority (600);

C.mas_makeConstrains, mas_remakeConstrains, mas_upDateConstrains
1.mas_makeConstrains can add one or more attributes on the view object. It will not affect other attributes wich we have added before.

[view1 mas_makeConstraints:^ (MASConstraintMaker *make) {self.topConstraint = make.top.equalTo (superview.mas_top).With.offset (padding.top); make.left.equalTo (superview.mas_left).With.offset (padding.left);}];

2.mas_remakeConstrains, can, add, a, of, constrains, on, the, view, but, in, the, same, time, it, will, reomove, all,, of, constrians, set, which, we, before., have, added

[self.button mas_remakeConstraints:^ (MASConstraintMaker *make) {make.size.equalTo (self.buttonSize); if (topLeft) {make.top.and.left.offset (10);} else {make.bottom.and.right.offset}}] (-10);

3.mas_updateConstrains, can, update, constrains, on, the, view

[self.growingButton mas_updateConstraints:^ (MASConstraintMaker *make) {make.center.equalTo (self); make.width.equalTo (@.PriorityLow) ((self.buttonSize.width)); make.height.equalTo (@.PriorityLow) ((self.buttonSize.height)); make.width.lessThanOrEqualTo (self); make.height.lessThanOrEqualTo (self);}];

The most important part

Masonry, is, useful, and, powerful, but, we, should, be, careful, of,, some, details, of, it.

  1. We, cannot, get, frame, immediately, after, set, constrains, by, Masonry, if, want, to, get, correct, frame, we, should, we, use, [view, setIfNeedLayout],, to, value, update, the, layout.
  2. Mas_updateConstraints, can, update, constrains, but, it, can, update, the, constains, which, we, set, before, and, the, have, relative, superview, must,, be, the, only, same, as, one., previouse