Using Masonry handwriting constraints

Source: Internet
Author: User

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); (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); (Superview). insets (Viewinsets); Make.width.equalTo (redview.mas_width). Offset (0); Make.height.equalTo (@ -);}];

The effect is as follows:

Using Masonry handwriting constraints

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: 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.