iOS layoutMargins 的坑:一個(gè)活久見的 bug

神奇的效果

有天一回到座位上,張皇失措的應(yīng)屆生同事就好像看到救星一樣把我抓過去:“倉(cāng)薯蚊夫,不好了,你看它這樣了E吵ⅰ这橙!”

我一看,從不說粗口的倉(cāng)薯也忍不住說了一句:“我……去导披,我做了這么多年 iOS 還從來沒遇見這樣的事屈扎。” 把領(lǐng)導(dǎo)也叫過來看撩匕。領(lǐng)導(dǎo)拿來玩了一會(huì)兒鹰晨,然后說:“哈哈哈,感覺真想要實(shí)現(xiàn)這個(gè)效果止毕,還不是那么容易呢……”

究竟是什么 bug 讓我們都這么不淡定呢模蜡?看下面的 gif 就知道了:

這個(gè)方塊形的 cell 就是一個(gè)平凡而普通的 collectionView 上平凡而普通的 collectionViewCell,很多地方都在用扁凛,用了一年多了忍疾,一直都長(zhǎng)這個(gè)樣子,從沒出任何問題谨朝。然而被我們的應(yīng)屆生同事不知道怎么一改卤妒,出現(xiàn)了這樣的效果:當(dāng) cell 滾動(dòng)到屏幕邊緣,即將離開屏幕的時(shí)候字币,它好像舍不得離開一樣则披,竟然把自己縮起來了……

要不要來幫我 debug

以下是能重現(xiàn) bug 的代碼,能在 iPhone 7 iOS 11 模擬器上重現(xiàn)洗出。為了只寫一個(gè)文件士复,我就把代碼最簡(jiǎn)化了,只要 60 行:

import UIKit

final class TestCell: UICollectionViewCell {

override init(frame: CGRect) {

let imageView = UIImageView(frame: .zero)

let metadataView = UIView(frame: .zero)

super.init(frame: frame)

imageView.backgroundColor = UIColor.red

metadataView.backgroundColor = UIColor.green

for view in [imageView, metadataView] {

addSubview(view)

view.translatesAutoresizingMaskIntoConstraints = false

view.leadingAnchor.constraint(equalTo: self.layoutMarginsGuide.leadingAnchor).isActive = true

view.trailingAnchor.constraint(equalTo: self.layoutMarginsGuide.trailingAnchor).isActive = true

}

imageView.topAnchor.constraint(equalTo: self.layoutMarginsGuide.topAnchor).isActive = true

imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true

metadataView.topAnchor.constraint(equalTo: imageView.bottomAnchor).isActive = true

metadataView.heightAnchor.constraint(equalToConstant: 25).isActive = true

metadataView.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor).isActive = true

}

required public init?(coder aDecoder: NSCoder) {

fatalError("init(coder:) has not been implemented")

}

}

final class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {

super.viewDidLoad()

self.collectionView!.contentInsetAdjustmentBehavior = .never

self.collectionView!.register(TestCell.self, forCellWithReuseIdentifier: "Cell")

}

// MARK: UICollectionViewDataSource

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

return 10

}

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

return collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

let measurementCell = TestCell()

let width = (collectionView.bounds.size.width - 20) / 2.0

measurementCell.widthAnchor.constraint(equalToConstant: width).isActive = true

return CGSize(width: width, height: measurementCell.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height)

}

}

約束用的是系統(tǒng)原生的寫法翩活,可能大家平時(shí)用第三方庫(kù)用得多阱洪,原生寫法反而不熟悉了便贵。簡(jiǎn)單解釋下,假設(shè)紅色是圖片冗荸,綠色是描述吧:

圖片左邊承璃、右邊、上面約束到父 view俏竞,高度 = 寬度

描述左邊绸硕、右邊、下面約束到父 view魂毁,高度固定 25玻佩,頂部貼著圖片底部

代碼出來了,能看出是什么問題嗎席楚?

幾個(gè)猜測(cè)

Q:是不是 layout 出什么問題了咬崔!

A:用的是最簡(jiǎn)單的 UICollectionViewFlowLayout 啊…… 沒 override 任何東西。

Q:是不是 constraint 沖突烦秩?

A:你看我約束得有啥問題垮斯?明明不會(huì)有任何沖突耶。

Q:Cell size 算得不對(duì)吧只祠?

A:最普通的自動(dòng)計(jì)算…… 打 log 來看算得是對(duì)的兜蠕。而且,就算是出了問題抛寝,滾動(dòng)的時(shí)候也不會(huì)實(shí)時(shí)計(jì)算 size 啊…… 它可是一邊滾一邊縮啊……

Q:view.leadingAnchor.constraint(equalTo: self.layoutMarginsGuide.leadingAnchor).isActive = true 這個(gè)self.layoutMarginsGuide.leadingAnchor是什么鬼熊杨,你就不能用self.leadingAnchor嗎?

A:你猜對(duì)了…… 因?yàn)橄胧∈赂?self.layoutMargins 所以約束到 layoutMarginsGuide盗舰,但確實(shí)如果改成約束到普通的self.leadingAnchor就不會(huì)有問題了晶府。

Q:這貨是不是只有什么特定情況才有的 bug,比如 iOS 11 或者 iPhoneX

A:沒錯(cuò)是 iOS 11 才有……任何手機(jī)都可以重現(xiàn)钻趋,但確實(shí)跟 iPhoneX 有點(diǎn)關(guān)系……

這下聰明的讀者猜出是什么問題了嗎川陆?:)

其實(shí)就是少了一行

要解決這個(gè)問題很簡(jiǎn)單,就是在 cell 的init方法里加一句

1self.insetsLayoutMarginsFromSafeArea = false

insetsLayoutMarginsFromSafeArea 這個(gè)屬性對(duì)于所有UIView默認(rèn)為YES(我覺得這點(diǎn)并不是太科學(xué))蛮位,當(dāng)它為YES的時(shí)候较沪,view 的 layoutMargins 會(huì)根據(jù) safeArea 進(jìn)行調(diào)整。這樣的話土至,即使把 layoutMargins 設(shè)置為一個(gè)固定值比如 layoutMargins = .zero购对,但是到了屏幕邊緣的時(shí)候,它的 margins 還是會(huì)逐漸變大陶因,本意應(yīng)該是為了讓子 view 自動(dòng)避開 iPhoneX 的劉海吧。這樣垂蜗,出現(xiàn)上面這個(gè)效果神奇的 bug也不足為怪了楷扬。

Layout Margins 的好處和坑

這么說的話解幽,其實(shí)應(yīng)該是個(gè)很常見的問題,為啥平常遇到的不多呢烘苹?我想還是因?yàn)槲覀兗s束到 layoutMarginsGuide 的情況比較少吧躲株。

layoutMargins 這套東西用來改 insets 是非常方便的。比如我寫一個(gè)用途很廣泛的東西镣衡,希望能支持使用者隨意改動(dòng)它的 insets霜定,如果我不用 layoutMargins 的話,我需要維護(hù) 4 個(gè) constraints:

// properties

var leadingInsetConstraint: NSLayoutConstraint!

var trailingInsetConstraint: NSLayoutConstraint!

var topConstraint: NSLayoutConstraint!

var bottomConstraint: NSLayoutConstraint!

// during init

self.leadingInsetConstraint = someView.leadingAnchor.constraint(equalTo: self.leadingAnchor)

self.leadingInsetConstraint.isActive = true

self.trailingInsetConstraint = someView.trailingAnchor.constraint(equalTo: self.trailingAnchor)

self.trailingInsetConstraint.isActive = true

self.topInsetConstraint = someView.topAnchor.constraint(equalTo: self.topAnchor)

self.topInsetConstraint.isActive = true

self.bottomInsetConstraint = someView.bottomAnchor.constraint(equalTo: self.bottomAnchor)

self.bottomInsetConstraint.isActive = true

// configuration

self.leadingInsetConstraint.constant = inset.left // 假設(shè)我們不考慮阿拉伯語(yǔ)吧

self.trailingInsetConstraint.constant = inset.right

self.topInsetConstraint.constant = inset.top

self.bottomInsetConstraint.constant = inset.bottom

而如果我用layoutMagins這套東西廊鸥,上面這些代碼就可以簡(jiǎn)化很多了望浩,一個(gè)屬性都不用存:

// during init

self.leadingInsetConstraint = someView.leadingAnchor.constraint(equalTo: self.layoutMarginsGuide.leadingAnchor)

self.trailingInsetConstraint = someView.trailingAnchor.constraint(equalTo: self.layoutMarginsGuide.trailingAnchor)

self.topInsetConstraint = someView.topAnchor.constraint(equalTo: self.layoutMarginsGuide.topAnchor)

self.bottomInsetConstraint = someView.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor)

// configuration

self.layoutMargins = insets

如果使用 directionalLayoutMargins,連阿拉伯語(yǔ)的情況都自動(dòng)處理好了惰说。

但它也有一些坑磨德,上面提到的就是其中之一。另外的我隨便列兩個(gè):

layoutMargins 的默認(rèn)值居然不是 0吆视。這一點(diǎn)讓我永遠(yuǎn)都不能理解蘋果的腦回路典挑,它的默認(rèn)值是 UIEdgeInsets(8,8,8,8)。也許 8 是某個(gè)蘋果工程師的幸運(yùn)數(shù)字吧……

沒有加進(jìn) view hierarchy 之前啦吧,布局可能無法正確使用 layout margins您觉。這一點(diǎn)就比較詭異,印象中以前就遇到需要先 addSubview 再設(shè) layoutMargins授滓,反過來就跟沒設(shè)一樣的神奇 bug琳水,也不知道最新版的系統(tǒng)修好了沒有了……

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市褒墨,隨后出現(xiàn)的幾起案子炫刷,更是在濱河造成了極大的恐慌,老刑警劉巖郁妈,帶你破解...
    沈念sama閱讀 221,820評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浑玛,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡噩咪,警方通過查閱死者的電腦和手機(jī)顾彰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來胃碾,“玉大人涨享,你說我怎么就攤上這事∑桶伲” “怎么了厕隧?”我有些...
    開封第一講書人閱讀 168,324評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我吁讨,道長(zhǎng)髓迎,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,714評(píng)論 1 297
  • 正文 為了忘掉前任建丧,我火速辦了婚禮排龄,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘翎朱。我一直安慰自己橄维,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評(píng)論 6 397
  • 文/花漫 我一把揭開白布拴曲。 她就那樣靜靜地躺著争舞,像睡著了一般。 火紅的嫁衣襯著肌膚如雪疗韵。 梳的紋絲不亂的頭發(fā)上兑障,一...
    開封第一講書人閱讀 52,328評(píng)論 1 310
  • 那天,我揣著相機(jī)與錄音蕉汪,去河邊找鬼流译。 笑死,一個(gè)胖子當(dāng)著我的面吹牛者疤,可吹牛的內(nèi)容都是我干的福澡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,897評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼驹马,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼革砸!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起糯累,我...
    開封第一講書人閱讀 39,804評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤算利,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后泳姐,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體效拭,經(jīng)...
    沈念sama閱讀 46,345評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評(píng)論 3 340
  • 正文 我和宋清朗相戀三年胖秒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了缎患。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,561評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡阎肝,死狀恐怖挤渔,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情风题,我是刑警寧澤判导,帶...
    沈念sama閱讀 36,238評(píng)論 5 350
  • 正文 年R本政府宣布嫉父,位于F島的核電站,受9級(jí)特大地震影響骡楼,放射性物質(zhì)發(fā)生泄漏熔号。R本人自食惡果不足惜稽鞭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評(píng)論 3 334
  • 文/蒙蒙 一鸟整、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧朦蕴,春花似錦篮条、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至疹娶,卻和暖如春伴栓,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背雨饺。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工钳垮, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人额港。 一個(gè)月前我還...
    沈念sama閱讀 48,983評(píng)論 3 376
  • 正文 我出身青樓饺窿,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親移斩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子肚医,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容