LHSCategoryCollection 0.0.22

LHSCategoryCollection 0.0.22

TestsTested
LangLanguage Obj-CObjective C
License Apache 2
ReleasedLast Release Aug 2015

Maintained by Dan Loewenherz.



  • By
  • Dan Loewenherz

This is a collection of helpful categories for use in iOS projects.

Usage

LHSCategoryCollection has a lot of great categories that are quick and easy to use. Here, I'm going to cover a few functions of the UIView+LHSAdditions category.

Necessary import and protocol for using the UIView+LHSAdditions of LHSCategoryCollection

#import <LHSCategoryCollection/UIView+LHSAdditions.h>

For these samples, we will be using three views

@property (nonatomic, strong) UIView *redBox;
@property (nonatomic, strong) UIView *greenBox;
@property (nonatomic, strong) UIView *blueBox;

Initialize your views in the viewDidLoad method

self.redBox = [[UIView alloc] init];
self.redBox.translatesAutoresizingMaskIntoConstraints = NO;
self.redBox.backgroundColor = [UIColor redColor];

self.greenBox = [[UIView alloc] init];
self.greenBox.translatesAutoresizingMaskIntoConstraints = NO;
self.greenBox.backgroundColor = [UIColor greenColor];

self.blueBox = [[UIView alloc] init];
self.blueBox.translatesAutoresizingMaskIntoConstraints = NO;
self.blueBox.backgroundColor = [UIColor blueColor];

To size a view with margins

[self.view addSubview:self.blueBox];
[self.blueBox lhs_expandToFillSuperviewWithVerticalMargin:20 horizontalMargin:20];

To center a view vertically and horizontally

[self.blueBox addSubview:self.redBox];
[UIView lhs_addConstraints:@"H:[view(100)]" views:@[self.redBox]];
[UIView lhs_addConstraints:@"V:[view(100)]" views:@[self.redBox]];
[self.blueBox lhs_centerHorizontallyForView:self.redBox];
[self.blueBox lhs_centerVerticallyForView:self.redBox];

The end result looks like this:

image

To position views with margins

NSDictionary *metrics = @{
                          @"verticalMargin": @(55),
                          @"horizontalMargin": @(15)
                          };


NSDictionary *views = @{
                        @"greenBox": self.greenBox,
                        @"redBox": self.redBox
                        };

[self.view addSubview:self.redBox];
[self.view addSubview:self.greenBox];

[UIView lhs_addConstraints:@"H:|-(horizontalMargin)-[view]-(horizontalMargin)-|" metrics:metrics views:@[self.redBox, self.greenBox]];
[self.redBox lhs_fillHeightOfSuperview];
[self.view lhs_addConstraints:@"V:|-(verticalMargin)-[greenBox]-(verticalMargin)-|" metrics:metrics views:views];

The end result looks like this:

image

Requirements

You will not have any issues if you are building for iOS 7 or higher.

Installation

LHSCategoryCollection is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "LHSCategoryCollection"

Author

Dan Loewenherz, [email protected]

License

LHSCategoryCollection is available under the Apache 2.0 license. See the LICENSE file for more info.