Quantcast
Channel: stunning co.de
Viewing all articles
Browse latest Browse all 20

Introducing NSLayoutConstraint Extensions.

$
0
0

CollisionNSLayoutConstraint Extensions is a useful category on NSLayoutConstraint containing a set of helper methods for creating most commonly used constraints. The category also includes a debugging extension that makes troubleshooting invalid constraints much easier and faster.

It is designed to be as easy to use as possible to integrate and use to speed up everyday development tasks. All the methods are well documented and described using doxygen style.

Furthermore, the category comes along with a small demo project where you can see how to use helper methods for creating and debugging constraints. Current version of the category includes helper methods for creating centering and filling constraints, constraints that set width and height and debugging extensions.

What for?

I was tired of writing same multiline, hard to read code to add just a constraint that pins width of the view. Same with centering constraints, constraints for filling other view with some padding etc. The code looked a bit messy even though I am super strict and clean when it comes to code formatting.

Example

Let’s compare usual way of programatically creating an auto layout constraint that sets width of a view:

NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:view
                                                     attribute:NSLayoutAttributeWidth
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:nil
                                                     attribute:NSLayoutAttributeNotAnAttribute
                                                    multiplier:1.0f
                                                      constant:100.0f];

It’s quite a lot of typing and what you end up is code that is rather hard to read. Let’s use NSLayoutConstraint Extensions for that:

NSLayoutConstraint *c = [NSLayoutConstraint constraintToSetWidth:100.0f forView:view];

Oh yeah, that’s what I like. Now you should understand what the category does. It is just a wrapper for most common auto layout constraints. It makes code look much cleaner.

NSLayoutConstraint Extensions

A list with all the helper methods available in NSLayoutConstraint Extensions with their descriptions can be found below.

Documentation

Creates constraints that align center of the view with the center of the reference view.

+ (NSArray *)constraintsToCenterView:(UIView *)viewToCenter
                   withReferenceView:(UIView *)referenceView;

Creates constraints that vertically align center of the view with the center of the reference view.

+ (NSLayoutConstraint *)constraintsToCenterVerticallyView:(UIView *)viewToCenter
                                        withReferenceView:(UIView *)referenceView;

Creates constraints that horizontally align center of the view with the center of the reference view.

+ (NSLayoutConstraint *)constraintToCenterHorizontallyView:(UIView *)viewToCenter
                                         withReferenceView:(UIView *)referenceView;

Creates constraints that horizontally fill the view with the second view.

+ (NSArray *)constraintsToFillHorizontallyView:(UIView *)viewToFill
                                      withView:(UIView *)view;

Creates constraints that horizontally fill the view with the second view leaving a padding on both sides.

+ (NSArray *)constraintsToFillHorizontallyView:(UIView *)viewToFill
                                      withView:(UIView *)view
                                    edgeInsets:(UIEdgeInsets)edgeInsets;

Creates constraints that vertically fill the view with the second view.

+ (NSArray *)constraintsToFillVerticallyView:(UIView *)viewToFill
                                    withView:(UIView *)view;

Creates constraints that vertically fill the view with the second view leaving a top and bottom padding.

+ (NSArray *)constraintsToFillVerticallyView:(UIView *)viewToFill
                                    withView:(UIView *)view
                                  edgeInsets:(UIEdgeInsets)edgeInsets;

Creates constraints that fill the view with the second view.

+ (NSArray *)constraintsToFillView:(UIView *)viewToFill
                          withView:(UIView *)view;

Creates constraints that fill the view with the second view.

+ (NSArray *)constraintsToFillView:(UIView *)viewToFill
                          withView:(UIView *)view
                        edgeInsets:(UIEdgeInsets)edgeInsets;

Creates constraint that pins width of the view.

- (NSLayoutConstraint *)constraintToSetWidth:(CGFloat)width
                                     forView:(UIView *)view;

Creates constraint that pins height of the view.

+ (NSLayoutConstraint *)constraintToSetHeight:(CGFloat)height
                                      forView:(UIView *)view;

Creates constraint that pins width and height of the view.

+ (NSArray *)constraintsToSetWidth:(CGFloat)width
                         andHeight:(CGFloat)height
                           forView:(UIView *)view;

Adding NSLayoutConstraint Extensions to Xcode Project

NSLayoutConstraint Extensions is a category. Therefore, there are only two files you need to add to your project.

NSLayoutConstraint+Extensions.h
NSLayoutConstraint+Extensions.m

There are 3 ways of adding them to Xcode project. I will show you all of them, starting with the fastest one.

Gist

Downloading files as a gist and drag and dropping them into your Xcode project is the fastest option. The files can be downloaded from this gist repo. Then, simply copy them into your project.

GitHub

The category as well as demo project can be checked out from its GitHub repository here. Please star the project if you like it to show support. That is the thing that drives me forward.

CocoaPods (Coming Soon)

If your project already uses CocoaPods for managing dependencies, all you have to do is add one line to the Podfile.

pod 'NSLayoutConstraint+Extensions'

After doing this, run pod install in Terminal and you are ready to go.

Have fun!

Have fun with it, let me know about any problems, ideas and request adding new animations in the comments.

The post Introducing NSLayoutConstraint Extensions. appeared first on stunning co.de.


Viewing all articles
Browse latest Browse all 20

Trending Articles