問題描述
我有一種情況,我正在使用 UIScrollView 來(lái)幫助使用較小手機(jī)的用戶查看窗口內(nèi)的全文.如果他們需要滾動(dòng),我想顯示一個(gè)圖像圖標(biāo)(只是為了讓他們知道窗口內(nèi)還有更多內(nèi)容),但我在使用 Swift 查找滾動(dòng)視圖的高度時(shí)遇到了問題.
這是我的對(duì)象的層次結(jié)構(gòu)
基本上,這 4 個(gè)標(biāo)簽適合 4.7 英寸 iPhone,但不適合 4 英寸 iPhone.我嘗試設(shè)置一些邏輯來(lái)檢查 UIScrollView 內(nèi)容是否大于窗口,但它似乎不起作用.我一直認(rèn)為高度為零.
我正在使用 scrollHeight = scrollView.contentSize.height
并嘗試將其放入 viewDidLoad()
、viewWillAppear()
、和 viewDidLayoutSubviews()
但每次它最終都是 0.
contentSize
是我應(yīng)該用來(lái)檢查高度的正確方法嗎?
TL;DR;
作為一般指南,自動(dòng)布局不會(huì)在調(diào)用 viewDidLoad
時(shí)完成為視圖提供大小(在 viewDidLayoutSubviews
中也不正確).在查詢 bounds
或 frame
之前,要確保所有視圖及其子視圖的大小正確,請(qǐng)執(zhí)行以下操作:
self.view.setNeedsLayout()self.view.layoutIfNeeded()print(self.someSubview.frame)//給出正確的幀
說(shuō)明
有點(diǎn)違反直覺,將代碼放入 viewDidLayoutSubviews
并不意味著所有子視圖都已通過自動(dòng)布局獲得正確的大小.引用
這里是代碼:
類 ViewController: UIViewController {@IBOutlet 弱 var childView:UIView!@IBOutlet 弱 var grandchildView:UIView!覆蓋 func viewDidLoad() {super.viewDidLoad()print("child", #function, childView.frame.size.width)打印(孫子",#function,grandchildView.frame.size.width)}覆蓋 func viewDidLayoutSubviews() {super.viewDidLayoutSubviews()print("child", #function, childView.frame.size.width)打印(孫子",#function,grandchildView.frame.size.width)}覆蓋 func viewWillAppear(_ animated: Bool) {super.viewWillAppear(動(dòng)畫)print("child", #function, childView.frame.size.width)打印(孫子",#function,grandchildView.frame.size.width)}覆蓋 func viewDidAppear(_ animated: Bool) {super.viewDidAppear(動(dòng)畫)print("child", #function, childView.frame.size.width)打印(孫子",#function,grandchildView.frame.size.width)}}
這是輸出:
子 viewDidLoad() 240.0孫子 viewDidLoad() 200.0子視圖將出現(xiàn) 240.0孫子 viewWillAppear 200.0child viewDidLayoutSubviews() 271.0//`child` 現(xiàn)在有它的最終寬度孫子 viewDidLayoutSubviews() 200.0//`grandchild` 仍然有它原來(lái)的寬度!//現(xiàn)在,在大多數(shù)情況下,所有視圖都將具有正確的大小.子viewDidAppear 271.0孫子 viewDidAppear 231.0
您可以從此輸出中看到,viewDidLayoutSubviews
不保證任何超出視圖控制器主視圖的直接子視圖的內(nèi)容都具有正確的大小.
注意:通常在調(diào)用 viewDidAppear
時(shí),視圖具有正確的大小,但我想說(shuō)這是絕對(duì)不能保證.
I have a situation where I'm using a UIScrollView to help users with smaller phones see the full text inside a window. And I was wanting to show an image icon if they needed to scroll (just so that they know there is more inside the window) but I'm having a problem finding the height of the scrollview using Swift.
This is the hierarchy of my objects
Basically, those 4 labels fit inside a 4.7" iPhone but not the 4" iPhone. I tried setting some logic to check if the UIScrollView content is larger than the window, but it doesn't seem to be working. I keep getting that the height is zero.
I'm using scrollHeight = scrollView.contentSize.height
and have tried putting that inside of viewDidLoad()
, viewWillAppear()
, and viewDidLayoutSubviews()
but every time it ends up being 0.
Is contentSize
the right thing I should be using to check the height?
TL;DR;
As a general guide, Auto Layout does not finish giving views their size by the time viewDidLoad
is called (neither is it correct inside viewDidLayoutSubviews
). To make sure all views and their subviews have their correct sizes before querying their bounds
or frame
, do:
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
print(self.someSubview.frame) // gives correct frame
Explanation
Somewhat counterintuitively, putting your code inside viewDidLayoutSubviews
does not mean all sub-views have been given their correct size by Auto Layout. To quote the docs for this method:
However, this method being called does not indicate that the individual layouts of the view's subviews have been adjusted.
What this means is that: inside viewDidLayoutSubviews
, only the first hierarchical-level of sub-views will have their correct size (i.e. child views but not "grandchild views").
Here is an example layout I created (the red view is the child, the blue view is the grandchild).
A few important points about this example:
- All views are positioned using Auto Layout constraints to their parent view (fixed margins, but not fixed widths), this allows them to grow or shrink depending on the screen size of the simulator I run this on.
- I've used an iPhone 7 sized view controller in my storyboard, and ran the example on an iPhone 7 Plus Simulator. This means I can be sure the views will grow somewhat and use this to determine whether or not a view has its original size or its final size.
Note that in Interface Builder, the original width of the child view (red) is 240 points and the original width of the grandchild view (blue) is 200 points.
And here is the code:
class ViewController: UIViewController {
@IBOutlet weak var childView: UIView!
@IBOutlet weak var grandchildView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
print("child ", #function, childView.frame.size.width)
print("grandchild", #function, grandchildView.frame.size.width)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("child ", #function, childView.frame.size.width)
print("grandchild", #function, grandchildView.frame.size.width)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("child ", #function, childView.frame.size.width)
print("grandchild", #function, grandchildView.frame.size.width)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("child ", #function, childView.frame.size.width)
print("grandchild", #function, grandchildView.frame.size.width)
}
}
And here is the output:
child viewDidLoad() 240.0
grandchild viewDidLoad() 200.0
child viewWillAppear 240.0
grandchild viewWillAppear 200.0
child viewDidLayoutSubviews() 271.0 // `child` has it's final width now
grandchild viewDidLayoutSubviews() 200.0 // `grandchild` still has it's original width!
// By now, in MOST cases, all views will have their correct sizes.
child viewDidAppear 271.0
grandchild viewDidAppear 231.0
What you can see from this output is that viewDidLayoutSubviews
does not guarantee that anything past the immediate sub-views of the View Controller's principal view have their correct size.
Note: Usually by the time viewDidAppear
is called, views have their correct size, but I'd say this is by no means guaranteed.
這篇關(guān)于在Swift中布局后獲取UIScrollView的高度的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!