久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在Swift中布局后獲取UIScrollView的高度

Get the height of the UIScrollView after layout in Swift(在Swift中布局后獲取UIScrollView的高度)
本文介紹了在Swift中布局后獲取UIScrollView的高度的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問題描述

我有一種情況,我正在使用 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 中也不正確).在查詢 boundsframe 之前,要確保所有視圖及其子視圖的大小正確,請(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)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設(shè)置滾動(dòng)視圖內(nèi)容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自動(dòng)滾動(dòng))
iOS UIScrollView Lazy Loading(iOS UIScrollView 延遲加載)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模擬器上運(yùn)行時(shí),使用 iOS 6.0 SDK 并為 iOS 5 Target 構(gòu)建會(huì)導(dǎo)致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創(chuàng)建部分屏幕 UIPageViewController)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
主站蜘蛛池模板: 黑人巨大精品欧美黑白配亚洲 | 久久亚洲精品国产精品紫薇 | 久久免费精品 | 亚洲欧美日韩精品久久亚洲区 | 国产精品久久久久久久久免费相片 | 成年人精品视频 | 免费a在线 | 中国一级特黄视频 | 精品国产免费一区二区三区演员表 | 日韩中文字幕视频在线观看 | 国产精品永久免费视频 | 成人在线免费网站 | 成在线人视频免费视频 | 日一区二区三区 | 欧美一区二区三区在线观看视频 | 韩日一区 | 91在线网| 户外露出一区二区三区 | 日韩欧美高清 | 午夜精品久久久久久久99黑人 | 狠狠爱视频 | 成人精品一区二区 | 中文字幕一区在线观看视频 | 精品不卡 | 日韩国产中文字幕 | 欧美精品一区二区三区视频 | 99视频免费在线观看 | 日韩在线观看中文字幕 | 日韩精品一区二区三区视频播放 | 九九热在线视频免费观看 | 91av在线视频观看 | 在线观看国产精品一区二区 | 久久久精品 | 成人av一区二区三区 | 久久久这里都是精品 | 久久九九网站 | 欧美日韩免费在线 | 在线免费观看毛片 | 国产精品自拍视频网站 | 日韩日韩日韩日韩日韩日韩日韩 | 四虎影院在线观看免费视频 |