In the iOS development process, handwriting contraints is a very painful thing, often so a lost function to write a lot of code, very prone to errors, and very inconvenient debugging. So only in the case can not be used to write contraints manual, generally in the storyboard, but storyboard is also a pit dad, especially in the work of SVN collaboration problems are not very annoying; but then I know masonry, Experience for some time, very useful, since then rarely use storyboard write UI.
Installing masonry
There are more than one way to install using masonry, but the recommended approach is to use cocoapods to manage it by adding a sentence in Podfile pod ‘Masonry‘
, and of course specifying the version: pod ‘Masonry‘, ‘~> 0.6.1‘
; for masonry such third-party libraries, May be involved in any page, so it is best to add in the prefix PCH file #import "Masonry.h"
, by default, the relevant resources in masonry have MAS prefixes, such as Mas_makeconstraints method, Mas_left attribute, etc. If you do not want to use the MAS prefix, you can #import "Masonry.h"
define a macro before you can, #define MAS_SHORTHAND
but it is not recommended because Mas_left is less prone to conflict with other resource names than left.
Masonry common skills Display multiple view on the same line
UIView *redview =({UIView*view =[[UIView alloc] init]; View.backgroundcolor=[Uicolor Redcolor]; View;}); [Self.view Addsubview:redview]; UIView*yellowview =({UIView*view =[[UIView alloc] init]; View.backgroundcolor=[Uicolor Yellowcolor]; View;}); [Self.view Addsubview:yellowview]; Uiedgeinsets viewinsets= Uiedgeinsetsmake ( -,Ten,0,Ten); [Redview mas_makeconstraints:^ (Masconstraintmaker *Make ) {UIView*superview =Self.view; Make.left.equalTo (Superview). insets (Viewinsets); Make.right.equalTo (yellowview.mas_left). Offset (-Ten); Make.top.equalTo (Superview). insets (Viewinsets); Make.width.equalTo (yellowview.mas_width). Offset (0); Make.height.equalTo (@ -);}]; [Yellowview mas_makeconstraints:^ (Masconstraintmaker *Make ) {UIView*superview =Self.view; Make.right.equalTo (Superview). insets (Viewinsets); Make.left.equalTo (redview.mas_right). Offset (Ten); Make.top.equalTo (Superview). insets (Viewinsets); Make.width.equalTo (redview.mas_width). Offset (0); Make.height.equalTo (@ -);}];
The effect is as follows:
Using Masonry handwriting constraints