Harness the power of AutoLayout NSLayoutConstraints with a simplified, chainable and expressive syntax. Supports iOS and OSX Auto Layout

Related tags

Layout Masonry
Overview

Masonry Build Status Coverage Status Carthage compatible Pod Version

Masonry is still actively maintained, we are committed to fixing bugs and merging good quality PRs from the wider community. However if you're using Swift in your project, we recommend using SnapKit as it provides better type safety with a simpler API.

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.

For examples take a look at the Masonry iOS Examples project in the Masonry workspace. You will need to run pod install after downloading.

What's wrong with NSLayoutConstraints?

Under the hood Auto Layout is a powerful and flexible way of organising and laying out your views. However creating constraints from code is verbose and not very descriptive. Imagine a simple example in which you want to have a view fill its superview but inset by 10 pixels on every side

UIView *superview = self.view;

UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1];

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[superview addConstraints:@[

    //view1 constraints
    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeTop
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeTop
                                multiplier:1.0
                                  constant:padding.top],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeLeft
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeLeft
                                multiplier:1.0
                                  constant:padding.left],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeBottom
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeBottom
                                multiplier:1.0
                                  constant:-padding.bottom],

    [NSLayoutConstraint constraintWithItem:view1
                                 attribute:NSLayoutAttributeRight
                                 relatedBy:NSLayoutRelationEqual
                                    toItem:superview
                                 attribute:NSLayoutAttributeRight
                                multiplier:1
                                  constant:-padding.right],

 ]];

Even with such a simple example the code needed is quite verbose and quickly becomes unreadable when you have more than 2 or 3 views. Another option is to use Visual Format Language (VFL), which is a bit less long winded. However the ASCII type syntax has its own pitfalls and its also a bit harder to animate as NSLayoutConstraint constraintsWithVisualFormat: returns an array.

Prepare to meet your Maker!

Heres the same constraints created using MASConstraintMaker

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);
}];

Or even shorter

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(superview).with.insets(padding);
}];

Also note in the first example we had to add the constraints to the superview [superview addConstraints:.... Masonry however will automagically add constraints to the appropriate view.

Masonry will also call view1.translatesAutoresizingMaskIntoConstraints = NO; for you.

Not all things are created equal

.equalTo equivalent to NSLayoutRelationEqual

.lessThanOrEqualTo equivalent to NSLayoutRelationLessThanOrEqual

.greaterThanOrEqualTo equivalent to NSLayoutRelationGreaterThanOrEqual

These three equality constraints accept one argument which can be any of the following:

1. MASViewAttribute

make.centerX.lessThanOrEqualTo(view2.mas_left);
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

2. UIView/NSView

if you want view.left to be greater than or equal to label.left :

//these two constraints are exactly the same
make.left.greaterThanOrEqualTo(label);
make.left.greaterThanOrEqualTo(label.mas_left);

3. NSNumber

Auto Layout allows width and height to be set to constant values. if you want to set view to have a minimum and maximum width you could pass a number to the equality blocks:

//width >= 200 && width <= 400
make.width.greaterThanOrEqualTo(@200);
make.width.lessThanOrEqualTo(@400)

However Auto Layout does not allow alignment attributes such as left, right, centerY etc to be set to constant values. So if you pass a NSNumber for these attributes Masonry will turn these into constraints relative to the view’s superview ie:

//creates view.left = view.superview.left + 10
make.left.lessThanOrEqualTo(@10)

Instead of using NSNumber, you can use primitives and structs to build your constraints, like so:

make.top.mas_equalTo(42);
make.height.mas_equalTo(20);
make.size.mas_equalTo(CGSizeMake(50, 100));
make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
make.left.mas_equalTo(view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0));

By default, macros which support autoboxing are prefixed with mas_. Unprefixed versions are available by defining MAS_SHORTHAND_GLOBALS before importing Masonry.

4. NSArray

An array of a mixture of any of the previous types

make.height.equalTo(@[view1.mas_height, view2.mas_height]);
make.height.equalTo(@[view1, view2]);
make.left.equalTo(@[view1, @100, view3.right]);

Learn to prioritize

.priority allows you to specify an exact priority

.priorityHigh equivalent to UILayoutPriorityDefaultHigh

.priorityMedium is half way between high and low

.priorityLow equivalent to UILayoutPriorityDefaultLow

Priorities are can be tacked on to the end of a constraint chain like so:

make.left.greaterThanOrEqualTo(label.mas_left).with.priorityLow();

make.top.equalTo(label.mas_top).with.priority(600);

Composition, composition, composition

Masonry also gives you a few convenience methods which create multiple constraints at the same time. These are called MASCompositeConstraints

edges

// make top, left, bottom, right equal view2
make.edges.equalTo(view2);

// make top = superview.top + 5, left = superview.left + 10,
//      bottom = superview.bottom - 15, right = superview.right - 20
make.edges.equalTo(superview).insets(UIEdgeInsetsMake(5, 10, 15, 20))

size

// make width and height greater than or equal to titleLabel
make.size.greaterThanOrEqualTo(titleLabel)

// make width = superview.width + 100, height = superview.height - 50
make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50))

center

// make centerX and centerY = button1
make.center.equalTo(button1)

// make centerX = superview.centerX - 5, centerY = superview.centerY + 10
make.center.equalTo(superview).centerOffset(CGPointMake(-5, 10))

You can chain view attributes for increased readability:

// All edges but the top should equal those of the superview
make.left.right.and.bottom.equalTo(superview);
make.top.equalTo(otherView);

Hold on for dear life

Sometimes you need modify existing constraints in order to animate or remove/replace constraints. In Masonry there are a few different approaches to updating constraints.

1. References

You can hold on to a reference of a particular constraint by assigning the result of a constraint make expression to a local variable or a class property. You could also reference multiple constraints by storing them away in an array.

// in public/private interface
@property (nonatomic, strong) MASConstraint *topConstraint;

...

// when making constraints
[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);
}];

...
// then later you can call
[self.topConstraint uninstall];

2. mas_updateConstraints

Alternatively if you are only updating the constant value of the constraint you can use the convience method mas_updateConstraints instead of mas_makeConstraints

// this is Apple's recommended place for adding/updating constraints
// this method can get called multiple times in response to setNeedsUpdateConstraints
// which can be called by UIKit internally or in your code if you need to trigger an update to your constraints
- (void)updateConstraints {
    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.equalTo(@(self.buttonSize.width)).priorityLow();
        make.height.equalTo(@(self.buttonSize.height)).priorityLow();
        make.width.lessThanOrEqualTo(self);
        make.height.lessThanOrEqualTo(self);
    }];

    //according to apple super should be called at end of method
    [super updateConstraints];
}

3. mas_remakeConstraints

mas_updateConstraints is useful for updating a set of constraints, but doing anything beyond updating constant values can get exhausting. That's where mas_remakeConstraints comes in.

mas_remakeConstraints is similar to mas_updateConstraints, but instead of updating constant values, it will remove all of its constraints before installing them again. This lets you provide different constraints without having to keep around references to ones which you want to remove.

- (void)changeButtonPosition {
    [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);
        }
    }];
}

You can find more detailed examples of all three approaches in the Masonry iOS Examples project.

When the ^&*!@ hits the fan!

Laying out your views doesn't always goto plan. So when things literally go pear shaped, you don't want to be looking at console output like this:

Unable to simultaneously satisfy constraints.....blah blah blah....
(
    "<NSLayoutConstraint:0x7189ac0 V:[UILabel:0x7186980(>=5000)]>",
    "<NSAutoresizingMaskLayoutConstraint:0x839ea20 h=--& v=--& V:[MASExampleDebuggingView:0x7186560(416)]>",
    "<NSLayoutConstraint:0x7189c70 UILabel:0x7186980.bottom == MASExampleDebuggingView:0x7186560.bottom - 10>",
    "<NSLayoutConstraint:0x7189560 V:|-(1)-[UILabel:0x7186980]   (Names: '|':MASExampleDebuggingView:0x7186560 )>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x7189ac0 V:[UILabel:0x7186980(>=5000)]>

Masonry adds a category to NSLayoutConstraint which overrides the default implementation of - (NSString *)description. Now you can give meaningful names to views and constraints, and also easily pick out the constraints created by Masonry.

which means your console output can now look like this:

Unable to simultaneously satisfy constraints......blah blah blah....
(
    "<NSAutoresizingMaskLayoutConstraint:0x8887740 MASExampleDebuggingView:superview.height == 416>",
    "<MASLayoutConstraint:ConstantConstraint UILabel:messageLabel.height >= 5000>",
    "<MASLayoutConstraint:BottomConstraint UILabel:messageLabel.bottom == MASExampleDebuggingView:superview.bottom - 10>",
    "<MASLayoutConstraint:ConflictingConstraint[0] UILabel:messageLabel.top == MASExampleDebuggingView:superview.top + 1>"
)

Will attempt to recover by breaking constraint
<MASLayoutConstraint:ConstantConstraint UILabel:messageLabel.height >= 5000>

For an example of how to set this up take a look at the Masonry iOS Examples project in the Masonry workspace.

Where should I create my constraints?

@implementation DIYCustomView

- (id)init {
    self = [super init];
    if (!self) return nil;

    // --- Create your views here ---
    self.button = [[UIButton alloc] init];

    return self;
}

// tell UIKit that you are using AutoLayout
+ (BOOL)requiresConstraintBasedLayout {
    return YES;
}

// this is Apple's recommended place for adding/updating constraints
- (void)updateConstraints {

    // --- remake/update constraints here
    [self.button remakeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(self.buttonSize.width));
        make.height.equalTo(@(self.buttonSize.height));
    }];
    
    //according to apple super should be called at end of method
    [super updateConstraints];
}

- (void)didTapButton:(UIButton *)button {
    // --- Do your changes ie change variables that affect your layout etc ---
    self.buttonSize = CGSize(200, 200);

    // tell constraints they need updating
    [self setNeedsUpdateConstraints];
}

@end

Installation

Use the orsome CocoaPods.

In your Podfile

pod 'Masonry'

If you want to use masonry without all those pesky 'mas_' prefixes. Add #define MAS_SHORTHAND to your prefix.pch before importing Masonry

#define MAS_SHORTHAND

Get busy Masoning

#import "Masonry.h"

Code Snippets

Copy the included code snippets to ~/Library/Developer/Xcode/UserData/CodeSnippets to write your masonry blocks at lightning speed!

mas_make -> [<#view#> mas_makeConstraints:^(MASConstraintMaker *make) { <#code#> }];

mas_update -> [<#view#> mas_updateConstraints:^(MASConstraintMaker *make) { <#code#> }];

mas_remake -> [<#view#> mas_remakeConstraints:^(MASConstraintMaker *make) { <#code#> }];

Features

  • Not limited to subset of Auto Layout. Anything NSLayoutConstraint can do, Masonry can do too!
  • Great debug support, give your views and constraints meaningful names.
  • Constraints read like sentences.
  • No crazy macro magic. Masonry won't pollute the global namespace with macros.
  • Not string or dictionary based and hence you get compile time checking.

TODO

  • Eye candy
  • Mac example project
  • More tests and examples
Comments
  • Masonry is malfunctioning in iOS 10 while compiling with XCode 8.

    Masonry is malfunctioning in iOS 10 while compiling with XCode 8.

    It worked well before I updated to iOS 10 and Xcode 8. For example,in a UITableViewCell subclass:

            UIImageView *imageView = [UIImageView new];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            [self.contentView addSubview: imageView];
            self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.top.mas_equalTo(15);
                make.height.mas_equalTo(imageView.mas_width).with.multipliedBy(9/16.0);
            }];
            self.pImageView = imageView;
    
            [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.top.mas_equalTo(0);
                make.bottom.mas_equalTo(imageView.mas_bottom).with.offset(15);
            }];
    

    This used to work like a charm in iOS 9 while compiling with XCode 7.x, but now it is not. Console shows this:

    [LayoutConstraints] Unable to simultaneously satisfy constraints.
        Probably at least one of the constraints in the following list is one you don't want. 
        Try this: 
            (1) look at each constraint and try to figure out which you don't expect; 
            (2) find the code that added the unwanted constraint or constraints and fix it. 
    (
        "<MASLayoutConstraint:0x1740b1dc0 UITableViewCellContentView:0x100546830.bottom == UIImageView:0x1005203b0.bottom + 15>",
        "<MASLayoutConstraint:0x1740b2120 UIImageView:0x1005203b0.left == UITableViewCellContentView:0x100546830.left>",
        "<MASLayoutConstraint:0x1740b2180 UIImageView:0x1005203b0.right == UITableViewCellContentView:0x100546830.right>",
        "<MASLayoutConstraint:0x1740b2240 UIImageView:0x1005203b0.top == UITableViewCellContentView:0x100546830.top + 15>",
        "<MASLayoutConstraint:0x1740b2300 UIImageView:0x1005203b0.height == UIImageView:0x1005203b0.width * 0.5625>",
        "<NSLayoutConstraint:0x174099eb0 UITableViewCellContentView:0x100546830.height == 0>",
        "<NSLayoutConstraint:0x174099e60 UITableViewCellContentView:0x100546830.width == 375>"
    )
    
    Will attempt to recover by breaking constraint 
    <MASLayoutConstraint:0x1740b2300 UIImageView:0x1005203b0.height == UIImageView:0x1005203b0.width * 0.5625>
    

    How can I fix this? Thanks a lot.

    opened by aquaibm 31
  • How to update the Xib's Constraints

    How to update the Xib's Constraints

    if i used code to creat a view and make it's constrains,it's works . but if i used by xib and update it's constrains ,the view's layout is very strange. when i creat view with code and add it's subviews which is creat by xib , finally the all views layout was perfect.

    opened by ZhipingYang 22
  • Compatiblity issue with Masonry and iOS 9

    Compatiblity issue with Masonry and iOS 9

    Since I've started to build my app with Xcode 7.01 the app fails to run on iOS 9.

    The following use of Masonry

        UIView* contentView = UIView.new;
        [self.vScrollView addSubview:contentView];
        [contentView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.vScrollView); 
            make.width.equalTo(self.centralView.width); 
        }];
    

    lets my application crash:

    2015-10-10 01:46:10.381 MyApp[7575:686159] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempting to add unsupported attribute: (null)'
    *** First throw call stack:
    (
        0   CoreFoundation                      0x04514a94 __exceptionPreprocess + 180
        1   libobjc.A.dylib                     0x03fd5e02 objc_exception_throw + 50
        2   CoreFoundation                      0x0451492a +[NSException raise:format:arguments:] + 138
        3   Foundation                          0x016743e6 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
        4   MyApp                               0x0014395a -[MASViewConstraint setSecondViewAttribute:] + 746
        5   MyApp                               0x00144abe __40-[MASViewConstraint equalToWithRelation]_block_invoke + 2110
        6   MyApp                               0x0013c78d __24-[MASConstraint equalTo]_block_invoke + 125
        7   MyApp                               0x00106acb __41-[CatalogViewController viewDidLoad]_block_invoke158 + 411
        8   MyApp                               0x0014c527 -[UIView(MASAdditions) mas_makeConstraints:] + 167
        9   MyApp                               0x0012adf5 -[UIView(MASShorthandAdditions) makeConstraints:] + 85
        10  MyApp                               0x0010603a -[CatalogViewController viewDidLoad] + 906
        11  UIKit                               0x0241cd74 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 44
        12  UIKit                               0x024218c2 -[UIViewController loadViewIfRequired] + 1556
        13  UIKit                               0x02421cf1 -[UIViewController view] + 35
        14  MyApp                               0x0012f4e5 -[ECSlidingViewController viewDidLoad] + 245
        15  UIKit                               0x0241cd74 -[UIViewController _sendViewDidLoadWithAppearanceProxyObjectTaggingEnabled] + 44
        16  UIKit                               0x024218c2 -[UIViewController loadViewIfRequired] + 1556
        17  UIKit                               0x02421cf1 -[UIViewController view] + 35
        18  UIKit                               0x022d4ccc -[UIWindow addRootViewControllerViewIfPossible] + 69
        19  UIKit                               0x022d53fd -[UIWindow _setHidden:forced:] + 324
        20  UIKit                               0x022d579d -[UIWindow _orderFrontWithoutMakingKey] + 49
        21  UIKit                               0x022e8b64 -[UIWindow makeKeyAndVisible] + 81
        22  UIKit                               0x02256b70 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 4190
        23  UIKit                               0x0225dec6 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1976
        24  UIKit                               0x02281905 __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke3171 + 68
        25  UIKit                               0x0225abae -[UIApplication workspaceDidEndTransaction:] + 163
        26  FrontBoardServices                  0x06370ccc __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
        27  FrontBoardServices                  0x063707a3 __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
        28  FrontBoardServices                  0x0638e1cb -[FBSSerialQueue _performNext] + 184
        29  FrontBoardServices                  0x0638e602 -[FBSSerialQueue _performNextFromRunLoopSource] + 52
        30  FrontBoardServices                  0x0638d8fe FBSSerialQueueRunLoopSourceHandler + 33
        31  CoreFoundation                      0x0442ee7f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
        32  CoreFoundation                      0x04424b0b __CFRunLoopDoSources0 + 523
        33  CoreFoundation                      0x04423f28 __CFRunLoopRun + 1032
        34  CoreFoundation                      0x04423866 CFRunLoopRunSpecific + 470
        35  CoreFoundation                      0x0442367b CFRunLoopRunInMode + 123
        36  UIKit                               0x0225a497 -[UIApplication _run] + 540
        37  UIKit                               0x0225fcc1 UIApplicationMain + 160
        38  MyApp                               0x000cc7da main + 138
        39  libdyld.dylib                       0x05f1aa21 start + 1
    )
    libc++abi.dylib: terminating with uncaught exception of type NSException
    

    What can I do to (temporarly or better permanently) solve the issue?

    As far as I have understood, there is a compatiblity issue with Masonry and iOS 9. Is this correct? If yes, what is your timeline to be compatible to iOS 9?

    question 
    opened by dj-at-work 19
  • Masonry built with Swift Support

    Masonry built with Swift Support

    Currently when trying to set constraints in Swift using the objc Masonry files, an error is generated: Cannot convert the expression's type '$T6' to type '() -> ((AnyObject!) -> MASContraint!)!'

    opened by scmyers11 19
  • Autoboxing for scalar/struct attribute values

    Autoboxing for scalar/struct attribute values

    Implemented my idea from #61. Allows you to write code like this (without using number literals and other ugly stuff):

    make.top.equalTo(42);
    make.height.equalTo(20);
    make.size.equalTo(CGSizeMake(50, 100));
    make.edges.equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
    

    Old code still works fine :)

    Things I'm gonna do before this is ready for merging:

    • [x] Add tests
    • [x] Update/add docs
    • [x] Update examples
    • [x] Implement the same thing for offset? This would effectively ditch centerOffset and pointOffset.
    opened by nickynick 15
  • using a constant in equalTo() and adding an offset doesn't work

    using a constant in equalTo() and adding an offset doesn't work

    The following code will not work as expected:

    make.width.equalTo(@200).with.offset(-40);
    

    Instead of the expected constant value of 160, this will set up a constraint with -40 as constant.

    opened by ahti 14
  • update height did not work ?

    update height did not work ?

    The height of a label is 122,when new height is 71,and i update this height to 71,then i call “layoutIfNeeded” . But this height still is 122.

    please see the picture.

    2 pic

    question 
    opened by JxbSir 13
  • iOS9: topLayoutGuide/bottomLayoutGuide cause crash

    iOS9: topLayoutGuide/bottomLayoutGuide cause crash

    Code in demo

    [topView makeConstraints:^(MASConstraintMaker *make) {
        UIView *topLayoutGuide = (id)self.topLayoutGuide;
        // topLayoutGuide cause exception
        make.top.equalTo(topLayoutGuide.mas_bottom);
        make.left.equalTo(self.view);
        make.right.equalTo(self.view);
        make.height.equalTo(@40);
    }];
    
    question 
    opened by zxx 13
  • Layout a number of buttons equally.

    Layout a number of buttons equally.

    I would like to layout 3 buttons (similar to a tab bar) with equal widths depending on the number of items to be displayed. is there any examples of this?

    opened by ericlewis 13
  • updateConstraints installs two width constraints instead of updating it

    updateConstraints installs two width constraints instead of updating it

    When setting a constraint equalTo a NSNumber, and afterwards setting it to equalTo(self) in an updateConstraints block, the constraint is not recognised as needed to be updated, and instead two constraints are added and an autolayout error (cannot satisfy constraints) is thrown, which shows two simultaneously installed width constraints.

    Example:

    [self.containerView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (something) {
                make.width.equalTo(@760);
            } else {
                make.width.equalTo(self);
            }
    }];
    

    A workaround i had to use was to take the width of self manually and wrap it in a NSNumber like this:

    [self.containerView mas_updateConstraints:^(MASConstraintMaker *make) {
            if (something) {
                make.width.equalTo(@760);
            } else {
                make.width.equalTo(@(self.bounds.size.width));
            }
    }];
    
    opened by megakode 13
  • Live reload possible in some way?

    Live reload possible in some way?

    After I started using classy.as for my styling I have quickly become addicted to the live reloading feature. I'm thinking if we could somehow do something similar for Masonry it would be awesome. I don't know how this would work thought.

    It's mostly my constant numbers I would like to change live. Today I use DCIntrospect (https://github.com/lukaswelte/DCIntrospect-ARC) to adjust frames in the simulator and then I have to remember the new constant values and change them in the code afterwards.

    Could we define all constants in a separate file that is watched by Masonry. So that changes in the constant file would be injected into the running simulator?

    opened by olegam 12
  • Add support for Swift Package Manager

    Add support for Swift Package Manager

    [email protected]:fredericgermain/Masonry.git can be added to a project using branch pr/swiftpm

    It would be nice to have swiftpm added in a new version

    Inspired by https://stackoverflow.com/a/67376388/1532175, I'm not a Package.swift pro

    opened by fredericgermain 5
  • the newest version is 1.1.0 , but the 1.1.0 is not newest code compare with the branch of master

    the newest version is 1.1.0 , but the 1.1.0 is not newest code compare with the branch of master

    New Issue Checklist

    🚫 If this template is not filled out your issue will be closed with no comment. 🚫

    • [x] I have looked at the Documentation
    • [x] I have filled out this issue template.

    Issue Info

    Info | Value | -------------------------|-------------------------------------| Platform | all Platform Version | all Masonry Version | 1.1.0

    Issue Description

    the newest version is 1.1.0 , but the 1.1.0 is not newest code compare with the branch of master,

    In particular the method of [mas_safeAreaLayoutGuide], It will craash when use 1.1.0

    please update pod @kouky @danielrhammond @tonyarnold @bjallen @shermanl

    1.1.0 screenshots

    1 1 0

    master screenshots

    master
    opened by leezhihua 0
  • 缩小控件高度的动画 控件会错位平移

    缩小控件高度的动画 控件会错位平移

    原始控件 [self.testLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(10); make.top.mas_equalTo(200); make.width.mas_equalTo(200); make.height.mas_equalTo(200); }]; 平移动画: [UIView animateWithDuration:5.0f animations:^{

        [self.testLabel mas_updateConstraints:^(MASConstraintMaker *make) {
    
            make.height.mas_equalTo(100);
        }];
        
        [self.view layoutIfNeeded];
        [self.view.superview layoutIfNeeded];
    }];
    

    控件会错位平移

    opened by diamondfive 0
Releases(v1.1.0)
Owner
null
FlexLayout adds a nice Swift interface to the highly optimized facebook/yoga flexbox implementation. Concise, intuitive & chainable syntax.

FlexLayout adds a nice Swift interface to the highly optimized Yoga flexbox implementation. Concise, intuitive & chainable syntax. Flexbox is an incre

layoutBox 1.7k Dec 30, 2022
An Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

KVConstraintKit KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift Installation Using CocoaPods

Keshav Vishwkarma 90 Sep 1, 2022
Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Not Apple Autolayout wrapper. Provides placeholders. Linux support.

CGLayout Powerful autolayout framework, that can manage UIView(NSView), CALayer and not rendered views. Has cross-hierarchy coordinate space. Implemen

Koryttsev Denis 45 Jun 28, 2022
MisterFusion is Swift DSL for AutoLayout. It is the extremely clear, but concise syntax, in addition, can be used in both Swift and Objective-C. Support Safe Area and Size Class.

MisterFusion MisterFusion makes more easier to use AutoLayout in Swift & Objective-C code. Features Simple And Concise Syntax Use in Swift and Objecti

Taiki Suzuki 316 Nov 17, 2022
Auto Layout (and manual layout) in one line.

Auto Layout (and manual layout) in one line. Quick Look view.bb.centerX().below(view2).size(100) It’s equivalent to iOS 9 API: view.centerXAnchor.cons

Javier Zhang 74 Oct 19, 2022
Auto Layout made easy with the Custom Layout.

Auto Layout made easy with the Custom Layout. Getting started CocoaPods CocoaPods is a dependency manager for Cocoa projects. You can install it with

Malith Nadeeshan 1 Jan 16, 2022
🏗 MondrianLayout - describing structured layout for AutoLayout

?? A DSL based layout builder for AutoLayout

Muukii 155 Dec 10, 2022
The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. Objective-C and Swift compatible.

The ultimate API for iOS & OS X Auto Layout — impressively simple, immensely powerful. PureLayout extends UIView/NSView, NSArray, and NSLayoutConstrai

PureLayout 7.6k Jan 6, 2023
Random-Colors-iOS - Random colors generator app with auto layout

Random Colors Random colors generator app with auto layout Demo demo.mp4 Depende

Adem Özcan 8 Mar 23, 2022
Swifty DSL for programmatic Auto Layout in iOS

WWLayout Easy to write auto layout constraints, with minimal extensions to standard namespaces. Feature Highlights Easy to use, readable API Backwards

WW Tech 49 Oct 2, 2022
Intuitive and powerful Auto Layout library

Align introduces a better alternative to Auto Layout anchors. Semantic. Align APIs focus on your goals, not the math behind Auto Layout constraints. P

Alexander Grebenyuk 338 Oct 18, 2022
Written in pure Swift, QuickLayout offers a simple and easy way to manage Auto Layout in code.

QuickLayout QuickLayout offers an additional way, to easily manage the Auto Layout using only code. You can harness the power of QuickLayout to align

Daniel Huri 243 Oct 28, 2022
Declarative Auto Layout in Swift, clean and simple

Tails Tails is a take on declarative Auto Layout. If you don't like typing (like me), it might be your kind of thing! Tails is written in Swift and cu

Nick Tymchenko 17 Jan 31, 2019
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast

Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]

layoutBox 2.1k Jan 2, 2023
Very simple swipe-to-dismiss, supporting Auto Layout and dynamic heights

PanelPresenter Add swipe-dismiss logic to your view controller, supporting Auto Layout and dynamic heights. Installation Add this package to your proj

Pim 3 Aug 23, 2022
A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:

Cartography ?? ?? Using Cartography, you can set up your Auto Layout constraints in declarative code and without any stringly typing! In short, it all

Robb Böhnke 7.3k Jan 4, 2023
Auto Layout made easy

EasyPeasy is a Swift framework that lets you create Auto Layout constraints programmatically without headaches and never ending boilerplate code. Besi

Carlos Vidal 1.9k Dec 23, 2022
Lightweight Swift framework for Apple's Auto-Layout

I am glad to share with you a lightweight Swift framework for Apple's Auto-Layout. It helps you write readable and compact UI code using simple API. A

null 349 Dec 20, 2022
A compact but full-featured Auto Layout DSL for Swift

Mortar allows you to create Auto Layout constraints using concise, simple code statements. Use this: view1.m_right |=| view2.m_left - 12.0 Instead of:

Jason Fieldman 83 Jan 29, 2022