Skip to content

Commit

Permalink
Add -[configureForAutoLayout] for initialized view
Browse files Browse the repository at this point in the history
This new API method allows you to set translatesAutoresizingMaskIntoConstraints to NO with a chainable method call (as self is returned). It is convenient when you want to use a specific factory method or initializer, and configure the view for use with auto layout on the same line.

For example:
UIButton *button = [[UIButton buttonWithType:UIButtonTypeSystem] configureForAutoLayout];

Closes #75
  • Loading branch information
vivianforzj authored and smileyborg committed Jul 18, 2015
1 parent f58e4b2 commit e2e75ab
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions PureLayout/PureLayout/ALView+PureLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@
/** Initializes and returns a new view that does not convert the autoresizing mask into constraints. */
- (instancetype)initForAutoLayout;

/** Configures an existing view to not convert the autoresizing mask into constraints and returns the view. */
- (instancetype)configureForAutoLayout;


#pragma mark Create Constraints Without Installing

Expand Down
9 changes: 9 additions & 0 deletions PureLayout/PureLayout/ALView+PureLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ - (instancetype)initForAutoLayout
return self;
}

/**
Configures an existing view to not convert the autoresizing mask into constraints and returns the view.
*/
- (instancetype)configureForAutoLayout
{
self.translatesAutoresizingMaskIntoConstraints = NO;
return self;
}


#pragma mark Create Constraints Without Installing

Expand Down
24 changes: 24 additions & 0 deletions PureLayout/PureLayoutTests/PureLayoutInstantiationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,28 @@ - (void)testInitForAutoLayout
XCTAssert(view.translatesAutoresizingMaskIntoConstraints == NO, @"The view returned from [[ALImageView alloc] initForAutoLayout] should not translate its autoresizing mask into constraints.");
}

/**
Test the -[configureForAutoLayout] method.
*/
- (void)testConfigureForAutoLayout
{
ALView *view = [[ALView alloc] init];
XCTAssert(view.translatesAutoresizingMaskIntoConstraints, @"By default, initialized views should translate their autoresizing mask into constraints.");
ALView *returnedView = [view configureForAutoLayout];
XCTAssert(view == returnedView, @"Calling -[view configureForAutoLayout] should return an identical reference to the same view.");
XCTAssert(returnedView.translatesAutoresizingMaskIntoConstraints == NO, @"The view returned from [view configureForAutoLayout] should not translate its autoresizing mask into constraints.");

view = [[ALLabel alloc] init];
XCTAssert(view.translatesAutoresizingMaskIntoConstraints, @"By default, initialized views should translate their autoresizing mask into constraints.");
returnedView = [view configureForAutoLayout];
XCTAssert(view == returnedView, @"Calling -[view configureForAutoLayout] should return an identical reference to the same view.");
XCTAssert(returnedView.translatesAutoresizingMaskIntoConstraints == NO, @"The view returned from [view configureForAutoLayout] should not translate its autoresizing mask into constraints.");

view = [[ALImageView alloc] init];
XCTAssert(view.translatesAutoresizingMaskIntoConstraints, @"By default, initialized views should translate their autoresizing mask into constraints.");
returnedView = [view configureForAutoLayout];
XCTAssert(view == returnedView, @"Calling -[view configureForAutoLayout] should return an identical reference to the same view.");
XCTAssert(returnedView.translatesAutoresizingMaskIntoConstraints == NO, @"The view returned from [view configureForAutoLayout] should not translate its autoresizing mask into constraints.");
}

@end

0 comments on commit e2e75ab

Please sign in to comment.