前兩天通過Xcode8提交的適配iOS10的應用新版本終于成功上線,然而在預定上線時間的頭一天下午才開始將Xcode升級到8.0版本,之后的一路坑終于摸索著過來了,其中推送、基本設置、相機等權限問題都有很多資料提供,而關于masonry適配問題,只是說之前iOS版本下對masonry使用所寫代碼并不規范不夠嚴謹,但也沒具體給出解決方案。
本人先是選擇一處出現問題的cell的自動布局改為預先計算frame模型來解決這個問題,也就是說放棄使用masonry,但的確是太耗費功夫,眼看第二天就要打包提交應用商店,不能忍,就繼續摸索masonry到底問題出在哪兒了,還好最后找出一種解決方案。下面以代碼來說明,首先貼出之前的代碼:
- (void)layoutSubviews {
[super layoutSubviews];
……
[self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo……;
make.height.mas_equalTo(kLineHeight);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo(self);
make.bottom.mas_equalTo(self.bottomLineView);
}];
}
這里是我平時使用masonry的寫法,也就是說self.contentView的bottom是根據self.bottomLineView計算出來的,這樣寫在iOS10之前完全沒問題,然而當升級到Xcode8之后再Run控制臺就出現了布局有問題,列舉一堆計算contentView高度時的約束,最后說self.contentView.height == 0,這就是問題所在了。多次嘗試修改,終于發現按照下面方式修改就沒問題了:
- (void)layoutSubviews {
[super layoutSubviews];
……
[self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.mas_equalTo……;
make.height.mas_equalTo(kLineHeight);
make.bottom.mas_equalTo(self.contentView);
}];
[self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.leading.trailing.bottom.mas_equalTo(self);
}];
}
顯然,我只是將原本self.contentView的bottom約束寫到了約束它的self.bottomLineView的約束條件中,而將self.contentView的約束條件改為四個邊全都等于cell自身(self),就這樣,問題就解決了……