Skip to content

Commit

Permalink
1. 更新Masonry版本。
Browse files Browse the repository at this point in the history
2. 增加iOS8的Self-Sizing特性。
3. 使用新的mas_topLayoutGuide和mas_bottomLayoutGuide。
  • Loading branch information
燕泽堃 committed Aug 9, 2015
1 parent b0fb925 commit 6ae274a
Show file tree
Hide file tree
Showing 26 changed files with 690 additions and 339 deletions.
39 changes: 24 additions & 15 deletions MasonryExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7706" systemVersion="14E46" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="XBC-uH-vAh">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7703"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
Expand Down Expand Up @@ -222,13 +223,16 @@
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="变高的Cell+简单的高度缓存" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="2" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="P1q-eI-HAd" userLabel="instructionLabel">
<rect key="frame" x="16" y="20" width="288" height="30"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="7" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="P1q-eI-HAd" userLabel="instructionLabel">
<rect key="frame" x="16" y="20" width="288" height="76"/>
<constraints>
<constraint firstAttribute="height" relation="greaterThanOrEqual" constant="30" id="DvT-OK-ArF"/>
<constraint firstAttribute="height" constant="30" id="nOc-zo-8pc"/>
<constraint firstAttribute="height" constant="76" id="nOc-zo-8pc"/>
</constraints>
<fontDescription key="fontDescription" type="boldSystem" pointSize="16"/>
<string key="text">变高的Cell+简单的高度缓存
方法1: 调用systemLayoutSizeFittingSize: 获取高度。
方法2: 使用iOS8的Self-Sizing特性</string>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
Expand All @@ -246,7 +250,7 @@
</connections>
</button>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" translatesAutoresizingMaskIntoConstraints="NO" id="Rqo-68-hJM">
<rect key="frame" x="16" y="58" width="288" height="464"/>
<rect key="frame" x="16" y="99" width="288" height="431"/>
<inset key="separatorInset" minX="0.0" minY="0.0" maxX="0.0" maxY="0.0"/>
</tableView>
</subviews>
Expand All @@ -256,9 +260,9 @@
<constraint firstItem="P1q-eI-HAd" firstAttribute="trailing" secondItem="qB6-oh-e1F" secondAttribute="trailingMargin" id="4Ld-QS-Ubq"/>
<constraint firstItem="P1q-eI-HAd" firstAttribute="leading" secondItem="qB6-oh-e1F" secondAttribute="leadingMargin" id="Io1-rc-fKP"/>
<constraint firstItem="m2P-jY-nH8" firstAttribute="top" secondItem="kLW-Yp-esd" secondAttribute="bottom" constant="8" id="VXx-Yl-MdQ"/>
<constraint firstItem="Rqo-68-hJM" firstAttribute="top" secondItem="P1q-eI-HAd" secondAttribute="bottom" constant="8" id="aZv-8m-dCH"/>
<constraint firstItem="Rqo-68-hJM" firstAttribute="top" secondItem="P1q-eI-HAd" secondAttribute="bottom" constant="3" id="aZv-8m-dCH"/>
<constraint firstItem="kLW-Yp-esd" firstAttribute="trailing" secondItem="qB6-oh-e1F" secondAttribute="trailingMargin" id="fDL-Ti-lwm"/>
<constraint firstItem="kLW-Yp-esd" firstAttribute="top" secondItem="Rqo-68-hJM" secondAttribute="bottom" constant="8" id="kew-cN-YlA"/>
<constraint firstItem="kLW-Yp-esd" firstAttribute="top" secondItem="Rqo-68-hJM" secondAttribute="bottom" id="kew-cN-YlA"/>
<constraint firstItem="kLW-Yp-esd" firstAttribute="leading" secondItem="qB6-oh-e1F" secondAttribute="leadingMargin" id="pzy-V0-TBv"/>
<constraint firstItem="P1q-eI-HAd" firstAttribute="top" secondItem="tqv-th-R3Z" secondAttribute="bottom" id="v2Q-ST-m4M"/>
<constraint firstItem="Rqo-68-hJM" firstAttribute="leading" secondItem="qB6-oh-e1F" secondAttribute="leadingMargin" id="z4W-I9-zIs"/>
Expand Down Expand Up @@ -306,7 +310,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="oCl-Q4-FW1">
<rect key="frame" x="16" y="269" width="288" height="30"/>
<rect key="frame" x="16" y="325" width="288" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="vmR-lT-Kdp"/>
</constraints>
Expand All @@ -318,7 +322,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nfb-C1-fk2">
<rect key="frame" x="16" y="345" width="288" height="30"/>
<rect key="frame" x="16" y="401" width="288" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="lqe-qb-exp"/>
</constraints>
Expand All @@ -330,7 +334,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="nuV-Dx-TPg">
<rect key="frame" x="16" y="307" width="288" height="30"/>
<rect key="frame" x="16" y="363" width="288" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="VeL-zf-qGh"/>
</constraints>
Expand All @@ -341,12 +345,17 @@
<action selector="showOrHideBottomBar:" destination="z5e-ob-Q1E" eventType="touchUpInside" id="yao-Sb-n05"/>
</connections>
</button>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="使用top(bottom)LayoutGuide确定当前ViewController的最佳显示范围。并且直接使用length属性,避免强制转换成UIView所带来的风险。" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="4" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="PXy-7G-7bI">
<rect key="frame" x="16" y="169" width="288" height="92"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="10" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="PXy-7G-7bI">
<rect key="frame" x="16" y="137" width="288" height="180"/>
<constraints>
<constraint firstAttribute="height" constant="92" id="1d6-M2-qSw"/>
<constraint firstAttribute="height" constant="180" id="1d6-M2-qSw"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<string key="text">使用top(bottom)LayoutGuide确定当前ViewController的最佳显示范围。

方法1: 直接使用length属性,避免强制转换成UIView所带来的风险。

方法2: 使用新的mas_topLayoutGuide和mas_bottomLayoutGuide</string>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
Expand All @@ -356,7 +365,7 @@
<constraint firstItem="nuV-Dx-TPg" firstAttribute="leading" secondItem="nIi-TN-XaZ" secondAttribute="leadingMargin" id="7Ty-zd-TKh"/>
<constraint firstItem="PXy-7G-7bI" firstAttribute="leading" secondItem="nIi-TN-XaZ" secondAttribute="leadingMargin" id="ANl-Mr-9ec"/>
<constraint firstItem="oCl-Q4-FW1" firstAttribute="trailing" secondItem="nIi-TN-XaZ" secondAttribute="trailingMargin" id="CGR-oK-qIA"/>
<constraint firstAttribute="centerY" secondItem="oCl-Q4-FW1" secondAttribute="centerY" id="E0a-b8-e4z"/>
<constraint firstAttribute="centerY" secondItem="oCl-Q4-FW1" secondAttribute="centerY" constant="-56" id="E0a-b8-e4z"/>
<constraint firstItem="nuV-Dx-TPg" firstAttribute="trailing" secondItem="nIi-TN-XaZ" secondAttribute="trailingMargin" id="FiX-IB-mgg"/>
<constraint firstItem="oCl-Q4-FW1" firstAttribute="leading" secondItem="nIi-TN-XaZ" secondAttribute="leadingMargin" id="GDt-Ll-fBD"/>
<constraint firstItem="nfb-C1-fk2" firstAttribute="trailing" secondItem="nIi-TN-XaZ" secondAttribute="trailingMargin" id="Oik-7C-E3U"/>
Expand Down
64 changes: 35 additions & 29 deletions MasonryExample/Case4ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
#import "Case4Cell.h"
#import "Case4DataEntity.h"

// 注释掉下面的宏定义,就是用“传统”的模板Cell计算高度
//#define IOS_8_NEW_FEATURE_SELF_SIZING

@interface Case4ViewController () <UITableViewDelegate, UITableViewDataSource>
@property(weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property(nonatomic, strong) NSArray *data;
@property (nonatomic, strong) NSArray *data;

@end

Expand All @@ -27,11 +30,13 @@ - (void)viewDidLoad {
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.estimatedRowHeight = 80.0f;


#ifdef IOS_8_NEW_FEATURE_SELF_SIZING
// iOS 8 的Self-sizing特性
if ([UIDevice currentDevice].systemVersion.integerValue > 7) {
_tableView.rowHeight = UITableViewAutomaticDimension;
}
#endif

// 注册Cell
[_tableView registerClass:[Case4Cell class] forCellReuseIdentifier:NSStringFromClass([Case4Cell class])];
Expand All @@ -54,34 +59,35 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if ([UIDevice currentDevice].systemVersion.integerValue > 7) {
// iOS 8 的Self-sizing特性
return UITableViewAutomaticDimension;

#ifdef IOS_8_NEW_FEATURE_SELF_SIZING
// iOS 8 的Self-sizing特性
return UITableViewAutomaticDimension;
#else
static Case4Cell *templateCell;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
templateCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([Case4Cell class])];
});

// 获取对应的数据
Case4DataEntity *dataEntity = _data[(NSUInteger) indexPath.row];

// 填充数据
[templateCell setupData:dataEntity];

// 判断高度是否已经计算过
if (dataEntity.cellHeight <= 0) {
// 根据当前数据,计算Cell的高度,注意+1
dataEntity.cellHeight = [templateCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
NSLog(@"Calculate height: %ld", (long) indexPath.row);
} else {
static Case4Cell *templateCell;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
templateCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([Case4Cell class])];
});

// 获取对应的数据
Case4DataEntity *dataEntity = _data[(NSUInteger) indexPath.row];

// 填充数据
[templateCell setupData:dataEntity];

// 判断高度是否已经计算过
if (dataEntity.cellHeight <= 0) {
// 根据当前数据,计算Cell的高度,注意+1
dataEntity.cellHeight = [templateCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
NSLog(@"Calculate height: %ld", (long)indexPath.row);
} else {
NSLog(@"Get cache %ld", (long)indexPath.row);
}

return dataEntity.cellHeight;
NSLog(@"Get cache %ld", (long) indexPath.row);
}

return dataEntity.cellHeight;
#endif
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Expand Down
32 changes: 21 additions & 11 deletions MasonryExample/Case5ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#import "Case5ViewController.h"
#import "Masonry.h"

// 注释掉这个宏定义,就直接使用length值做约束,否则的话,使用新的mas_topLayoutGuide和mas_bottomLayoutGuide
#define NEW_FEATURE

@interface Case5ViewController ()
@property (strong, nonatomic) UIView *topView;
@property (strong, nonatomic) UIView *bottomView;
Expand All @@ -24,6 +27,8 @@ - (void)viewDidLoad {
self.title = @"Case 5";

[self initView];

NSLog(@"[viewDidLoad] top: %g", self.topLayoutGuide.length);
}

#pragma mark - Actions
Expand Down Expand Up @@ -53,9 +58,11 @@ - (void)initView {
[_topView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@40);
make.left.and.right.equalTo(self.view);
// 此处没有像Masonry的Example中一样,把topLayoutGuide强制转化成UIView
// 直接利用其length属性,避免iOS、SDK版本升级后topLayoutGuide不再是UIView
make.top.equalTo(self.view.mas_top).with.offset(self.topLayoutGuide.length);

#ifdef NEW_FEATURE
make.top.equalTo(self.mas_topLayoutGuide);
#endif

}];

_bottomView = [UIView new];
Expand All @@ -64,29 +71,32 @@ - (void)initView {
[_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@40);
make.left.and.right.equalTo(self.view);
// 此处没有像Masonry的Example中一样,把topLayoutGuide强制转化成UIView
// 直接利用其length属性,避免iOS、SDK版本升级后topLayoutGuide不再是UIView
make.bottom.equalTo(self.view.mas_bottom).with.offset(-(self.bottomLayoutGuide.length));

#ifdef NEW_FEATURE
make.bottom.equalTo(self.mas_bottomLayoutGuide);
#endif
}];
}

#pragma mark - Override

- (void)updateViewConstraints {
[super updateViewConstraints];

NSLog(@"Top: %g", self.topLayoutGuide.length);
NSLog(@"Bottom: %g", self.bottomLayoutGuide.length);


#ifndef NEW_FEATURE
// 根据新的length值更新约束
[_topView mas_updateConstraints:^(MASConstraintMaker *make) {
// 直接利用其length属性,避免iOS、SDK版本升级后topLayoutGuide不再是UIView
make.top.equalTo(self.view.mas_top).with.offset(self.topLayoutGuide.length);
}];

// 根据新的length值更新约束
[_bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
// 直接利用其length属性,避免iOS、SDK版本升级后topLayoutGuide不再是UIView
make.bottom.equalTo(self.view.mas_bottom).with.offset(-(self.bottomLayoutGuide.length));
}];
#endif

[super updateViewConstraints];
}

@end
6 changes: 3 additions & 3 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PODS:
- Masonry (0.6.1)
- Masonry (0.6.2)

DEPENDENCIES:
- Masonry

SPEC CHECKSUMS:
Masonry: 4972309f2f134de9dd312f4dc4a21359b50e6caa
Masonry: 362e8a1cc0beada4a4c4832d5e863da2a51533be

COCOAPODS: 0.36.3
COCOAPODS: 0.37.2
1 change: 1 addition & 0 deletions Pods/Headers/Private/Masonry/ViewController+MASAdditions.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Pods/Headers/Public/Masonry/ViewController+MASAdditions.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Pods/Masonry/Masonry/MASConstraint.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Pods/Masonry/Masonry/MASConstraint.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6ae274a

Please sign in to comment.