Skip to content

Commit b1a0bc3

Browse files
Tyler FoxTyler Fox
authored andcommitted
Improve sample project and fix bugs with autorotation
1 parent de57a1a commit b1a0bc3

5 files changed

Lines changed: 74 additions & 102 deletions

File tree

TableViewCellWithAutoLayout.xcodeproj/project.pbxproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
99BCDCDA18008C1700B8E66B /* RJModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 99BCDCD518008C1700B8E66B /* RJModel.m */; };
2323
99BCDCDB18008C1700B8E66B /* RJTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 99BCDCD718008C1700B8E66B /* RJTableViewCell.m */; };
2424
99BCDCDC18008C1700B8E66B /* RJTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 99BCDCD918008C1700B8E66B /* RJTableViewController.m */; };
25+
B147F1841867A715002B0C25 /* UIView+AutoLayout.m in Sources */ = {isa = PBXBuildFile; fileRef = B147F1831867A715002B0C25 /* UIView+AutoLayout.m */; };
2526
/* End PBXBuildFile section */
2627

2728
/* Begin PBXContainerItemProxy section */
@@ -57,6 +58,8 @@
5758
99BCDCD718008C1700B8E66B /* RJTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RJTableViewCell.m; sourceTree = "<group>"; };
5859
99BCDCD818008C1700B8E66B /* RJTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RJTableViewController.h; sourceTree = "<group>"; };
5960
99BCDCD918008C1700B8E66B /* RJTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RJTableViewController.m; sourceTree = "<group>"; };
61+
B147F1821867A715002B0C25 /* UIView+AutoLayout.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+AutoLayout.h"; sourceTree = "<group>"; };
62+
B147F1831867A715002B0C25 /* UIView+AutoLayout.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+AutoLayout.m"; sourceTree = "<group>"; };
6063
/* End PBXFileReference section */
6164

6265
/* Begin PBXFrameworksBuildPhase section */
@@ -116,6 +119,7 @@
116119
99BCDCAA18008C0000B8E66B /* TableViewCellWithAutoLayout */ = {
117120
isa = PBXGroup;
118121
children = (
122+
B147F1811867A715002B0C25 /* UIView+AutoLayout */,
119123
99BCDCB318008C0000B8E66B /* RJAppDelegate.h */,
120124
99BCDCB418008C0000B8E66B /* RJAppDelegate.m */,
121125
99BCDCB618008C0000B8E66B /* Images.xcassets */,
@@ -167,6 +171,15 @@
167171
path = TableViewController;
168172
sourceTree = "<group>";
169173
};
174+
B147F1811867A715002B0C25 /* UIView+AutoLayout */ = {
175+
isa = PBXGroup;
176+
children = (
177+
B147F1821867A715002B0C25 /* UIView+AutoLayout.h */,
178+
B147F1831867A715002B0C25 /* UIView+AutoLayout.m */,
179+
);
180+
path = "UIView+AutoLayout";
181+
sourceTree = "<group>";
182+
};
170183
/* End PBXGroup section */
171184

172185
/* Begin PBXNativeTarget section */
@@ -264,6 +277,7 @@
264277
buildActionMask = 2147483647;
265278
files = (
266279
99BCDCB518008C0000B8E66B /* RJAppDelegate.m in Sources */,
280+
B147F1841867A715002B0C25 /* UIView+AutoLayout.m in Sources */,
267281
99BCDCDC18008C1700B8E66B /* RJTableViewController.m in Sources */,
268282
99BCDCB118008C0000B8E66B /* main.m in Sources */,
269283
99BCDCDA18008C1700B8E66B /* RJModel.m in Sources */,
@@ -470,6 +484,7 @@
470484
99BCDCCF18008C0000B8E66B /* Release */,
471485
);
472486
defaultConfigurationIsVisible = 0;
487+
defaultConfigurationName = Release;
473488
};
474489
99BCDCD018008C0000B8E66B /* Build configuration list for PBXNativeTarget "TableViewCellWithAutoLayoutTests" */ = {
475490
isa = XCConfigurationList;
@@ -478,6 +493,7 @@
478493
99BCDCD218008C0000B8E66B /* Release */,
479494
);
480495
defaultConfigurationIsVisible = 0;
496+
defaultConfigurationName = Release;
481497
};
482498
/* End XCConfigurationList section */
483499
};

TableViewCellWithAutoLayout/TableViewCellWithAutoLayout-Prefix.pch

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
#ifdef __OBJC__
1414
#import <UIKit/UIKit.h>
1515
#import <Foundation/Foundation.h>
16+
#import "UIView+AutoLayout.h"
1617
#endif

TableViewCellWithAutoLayout/TableViewController/RJModel.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ - (void)addSingleItemToDataSource {
8383

8484
- (NSString *)randomLorumIpsum {
8585

86-
NSString *lorumIpsum = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent non quam ac massa viverra semper. Maecenas mattis justo ac augue volutpat congue. Maecenas laoreet, nulla eu faucibus gravida, felis orci dictum risus, sed sodales sem eros eget risus. Morbi imperdiet sed diam et sodales. Vestibulum ut est id mauris ultrices gravida. Nulla malesuada metus ut erat malesuada, vitae ornare neque semper. Aenean a commodo justo, vel placerat odio. Curabitur vitae consequat tortor. Aenean eu magna ante. Integer tristique elit ac augue laoreet, eget pulvinar lacus dictum. Cras eleifend lacus eget pharetra elementum. Etiam fermentum eu felis eu tristique. Integer eu purus vitae turpis blandit consectetur. Nulla facilisi. Praesent bibendum massa eu metus pulvinar, quis tristique nunc commodo. Ut varius aliquam elit, a tincidunt elit aliquam non. Nunc ac leo purus. Proin condimentum placerat ligula, at tristique neque scelerisque ut. Suspendisse ut congue enim. Integer id sem nisl. Nam dignissim, lectus et dictum sollicitudin, libero augue ullamcorper justo, nec consectetur dolor arcu sed justo. Proin rutrum pharetra lectus, vel gravida ante venenatis sed. Mauris lacinia urna vehicula felis aliquet venenatis. Suspendisse non pretium sapien. Proin id dolor ultricies, dictum augue non, euismod ante. Vivamus et luctus augue, a luctus mi. Maecenas sit amet felis in magna vestibulum viverra vel ut est. Suspendisse potenti. Morbi nec odio pretium lacus laoreet volutpat sit amet at ipsum. Etiam pretium purus vitae tortor auctor, quis cursus metus vehicula. Integer ultricies facilisis arcu, non congue orci pharetra quis. Vivamus pulvinar ligula neque, et vehicula ipsum euismod quis. Aliquam ut mi elementum, malesuada velit ac, placerat leo. Donec vel neque condimentum, congue justo a, posuere tortor. Etiam mollis id ligula nec dapibus. Etiam tincidunt, nisi non cursus adipiscing, enim neque tincidunt leo, vel tincidunt quam leo non ligula. Proin a felis tellus. Pellentesque quis purus est. Nam consectetur erat quam, non ultricies tortor venenatis ac. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Pellentesque at laoreet arcu. Mauris odio lorem, luctus facilisis ligula eget, malesuada pellentesque nulla.";
86+
NSString *lorumIpsum = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent non quam ac massa viverra semper. Maecenas mattis justo ac augue volutpat congue. Maecenas laoreet, nulla eu faucibus gravida, felis orci dictum risus, sed sodales sem eros eget risus. Morbi imperdiet sed diam et sodales. Vestibulum ut est id mauris ultrices gravida. Nulla malesuada metus ut erat malesuada, vitae ornare neque semper. Aenean a commodo justo, vel placerat odio. Curabitur vitae consequat tortor. Aenean eu magna ante. Integer tristique elit ac augue laoreet, eget pulvinar lacus dictum. Cras eleifend lacus eget pharetra elementum. Etiam fermentum eu felis eu tristique. Integer eu purus vitae turpis blandit consectetur. Nulla facilisi. Praesent bibendum massa eu metus pulvinar, quis tristique nunc commodo. Ut varius aliquam elit, a tincidunt elit aliquam non. Nunc ac leo purus. Proin condimentum placerat ligula, at tristique neque scelerisque ut. Suspendisse ut congue enim. Integer id sem nisl. Nam dignissim, lectus et dictum sollicitudin, libero augue ullamcorper justo, nec consectetur dolor arcu sed justo. Proin rutrum pharetra lectus, vel gravida ante venenatis sed. Mauris lacinia urna vehicula felis aliquet venenatis. Suspendisse non pretium sapien. Proin id dolor ultricies, dictum augue non, euismod ante. Vivamus et luctus augue, a luctus mi. Maecenas sit amet felis in magna vestibulum viverra vel ut est. Suspendisse potenti. Morbi nec odio pretium lacus laoreet volutpat sit amet at ipsum. Etiam pretium purus vitae tortor auctor, quis cursus metus vehicula. Integer ultricies facilisis arcu, non congue orci pharetra quis. Vivamus pulvinar ligula neque, et vehicula ipsum euismod quis.";
8787

8888
// Split lorum ipsum words into an array
8989
//
@@ -92,6 +92,7 @@ - (NSString *)randomLorumIpsum {
9292
// Randomly choose words for variable length
9393
//
9494
int r = arc4random() % [lorumIpsumArray count];
95+
r = MAX(3, r); // no less than 3 words
9596
NSArray *lorumIpsumRandom = [lorumIpsumArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, r)]];
9697

9798
// Array to string. Adding '!!!' to end of string to ensure all text is visible.

TableViewCellWithAutoLayout/TableViewController/RJTableViewCell.m

Lines changed: 34 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626

2727
#import "RJTableViewCell.h"
2828

29-
#define kLabelHorizontalInsets 20.0f
29+
#define kLabelHorizontalInsets 15.0f
30+
#define kLabelVerticalInsets 10.0f
3031

3132
@interface RJTableViewCell ()
3233

@@ -40,23 +41,22 @@ - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reus
4041
{
4142
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
4243
if (self) {
43-
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
44-
[self.titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
44+
self.titleLabel = [UILabel newAutoLayoutView];
4545
[self.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
4646
[self.titleLabel setNumberOfLines:1];
4747
[self.titleLabel setTextAlignment:NSTextAlignmentLeft];
4848
[self.titleLabel setTextColor:[UIColor blackColor]];
49-
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
49+
self.titleLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.1];
5050

51-
self.bodyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
52-
[self.bodyLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
53-
[self.bodyLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
51+
self.bodyLabel = [UILabel newAutoLayoutView];
5452
[self.bodyLabel setLineBreakMode:NSLineBreakByTruncatingTail];
5553
[self.bodyLabel setNumberOfLines:0];
5654
[self.bodyLabel setTextAlignment:NSTextAlignmentLeft];
5755
[self.bodyLabel setTextColor:[UIColor darkGrayColor]];
58-
[self.bodyLabel setBackgroundColor:[UIColor clearColor]];
59-
56+
self.bodyLabel.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.1];
57+
58+
self.contentView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.1];
59+
6060
[self.contentView addSubview:self.titleLabel];
6161
[self.contentView addSubview:self.bodyLabel];
6262

@@ -70,74 +70,36 @@ - (void)updateConstraints
7070
{
7171
[super updateConstraints];
7272

73-
if (self.didSetupConstraints) return;
74-
75-
[self.contentView addConstraint:[NSLayoutConstraint
76-
constraintWithItem:self.titleLabel
77-
attribute:NSLayoutAttributeLeading
78-
relatedBy:NSLayoutRelationEqual
79-
toItem:self.contentView
80-
attribute:NSLayoutAttributeLeading
81-
multiplier:1.0f
82-
constant:kLabelHorizontalInsets]];
83-
84-
[self.contentView addConstraint:[NSLayoutConstraint
85-
constraintWithItem:self.titleLabel
86-
attribute:NSLayoutAttributeTop
87-
relatedBy:NSLayoutRelationEqual
88-
toItem:self.contentView
89-
attribute:NSLayoutAttributeTop
90-
multiplier:1.0f
91-
constant:(kLabelHorizontalInsets / 2)]];
92-
93-
[self.contentView addConstraint:[NSLayoutConstraint
94-
constraintWithItem:self.titleLabel
95-
attribute:NSLayoutAttributeTrailing
96-
relatedBy:NSLayoutRelationEqual
97-
toItem:self.contentView
98-
attribute:NSLayoutAttributeTrailing
99-
multiplier:1.0f
100-
constant:-kLabelHorizontalInsets]];
101-
102-
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
73+
if (self.didSetupConstraints) {
74+
return;
75+
}
10376

104-
[self.contentView addConstraint:[NSLayoutConstraint
105-
constraintWithItem:self.bodyLabel
106-
attribute:NSLayoutAttributeLeading
107-
relatedBy:NSLayoutRelationEqual
108-
toItem:self.contentView
109-
attribute:NSLayoutAttributeLeading
110-
multiplier:1.0f
111-
constant:kLabelHorizontalInsets]];
77+
[self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
78+
[self.titleLabel autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:kLabelVerticalInsets];
79+
[self.titleLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:kLabelHorizontalInsets];
80+
[self.titleLabel autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:kLabelHorizontalInsets];
11281

113-
[self.contentView addConstraint:[NSLayoutConstraint
114-
constraintWithItem:self.bodyLabel
115-
attribute:NSLayoutAttributeTop
116-
relatedBy:NSLayoutRelationEqual
117-
toItem:self.titleLabel
118-
attribute:NSLayoutAttributeBottom
119-
multiplier:1.0f
120-
constant:(kLabelHorizontalInsets / 4)]];
82+
[self.bodyLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
83+
[self.bodyLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabel withOffset:kLabelVerticalInsets];
84+
[self.bodyLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:kLabelHorizontalInsets];
85+
[self.bodyLabel autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:kLabelHorizontalInsets];
86+
[self.bodyLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:kLabelVerticalInsets];
12187

122-
[self.contentView addConstraint:[NSLayoutConstraint
123-
constraintWithItem:self.bodyLabel
124-
attribute:NSLayoutAttributeTrailing
125-
relatedBy:NSLayoutRelationEqual
126-
toItem:self.contentView
127-
attribute:NSLayoutAttributeTrailing
128-
multiplier:1.0f
129-
constant:-kLabelHorizontalInsets]];
88+
self.didSetupConstraints = YES;
89+
}
90+
91+
- (void)layoutSubviews
92+
{
93+
[super layoutSubviews];
13094

131-
[self.contentView addConstraint:[NSLayoutConstraint
132-
constraintWithItem:self.bodyLabel
133-
attribute:NSLayoutAttributeBottom
134-
relatedBy:NSLayoutRelationEqual
135-
toItem:self.contentView
136-
attribute:NSLayoutAttributeBottom
137-
multiplier:1.0f
138-
constant:-(kLabelHorizontalInsets / 2)]];
95+
// The below call to layoutSubviews on the table view cell's contentView should NOT be necessary.
96+
// However, in some (but not all!) cases it appears as though the super implementation does not call
97+
// layoutSubviews on the contentView, which causes all the UILabels to have a frame of CGRectZero.
98+
[self.contentView layoutSubviews];
13999

140-
self.didSetupConstraints = YES;
100+
// Set the preferredMaxLayoutWidth of the mutli-line bodyLabel based on the evaluated width of the label's frame,
101+
// as this will allow the text to wrap correctly, and as a result allow the label to take on the correct height.
102+
self.bodyLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.bodyLabel.frame);
141103
}
142104

143105
- (void)updateFonts
@@ -146,9 +108,4 @@ - (void)updateFonts
146108
self.bodyLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
147109
}
148110

149-
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
150-
[super setSelected:selected animated:animated];
151-
// Configure the view for the selected state
152-
}
153-
154111
@end

0 commit comments

Comments
 (0)