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

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

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

問題描述

我有一種情況,我正在使用 UIScrollView 來幫助使用較小手機(jī)的用戶查看窗口內(nèi)的全文.如果他們需要滾動,我想顯示一個圖像圖標(biāo)(只是為了讓他們知道窗口內(nèi)還有更多內(nèi)容),但我在使用 Swift 查找滾動視圖的高度時遇到了問題.

這是我的對象的層次結(jié)構(gòu)

基本上,這 4 個標(biāo)簽適合 4.7 英寸 iPhone,但不適合 4 英寸 iPhone.我嘗試設(shè)置一些邏輯來檢查 UIScrollView 內(nèi)容是否大于窗口,但它似乎不起作用.我一直認(rèn)為高度為零.

我正在使用 scrollHeight = scrollView.contentSize.height 并嘗試將其放入 viewDidLoad()viewWillAppear()、和 viewDidLayoutSubviews() 但每次它最終都是 0.

contentSize 是我應(yīng)該用來檢查高度的正確方法嗎?

解決方案

TL;DR;

作為一般指南,自動布局不會在調(diào)用 viewDidLoad 時完成為視圖提供大小(在 viewDidLayoutSubviews 中也不正確).在查詢 boundsframe 之前,要確保所有視圖及其子視圖的大小正確,請執(zhí)行以下操作:

self.view.setNeedsLayout()self.view.layoutIfNeeded()print(self.someSubview.frame)//給出正確的幀

說明

有點(diǎn)違反直覺,將代碼放入 viewDidLayoutSubviews 并不意味著所有子視圖都已通過自動布局獲得正確的大小.引用

這里是代碼:

類 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(動畫)print("child", #function, childView.frame.size.width)打印(孫子",#function,grandchildView.frame.size.width)}覆蓋 func viewDidAppear(_ animated: Bool) {super.viewDidAppear(動畫)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` 仍然有它原來的寬度!//現(xiàn)在,在大多數(shù)情況下,所有視圖都將具有正確的大小.子viewDidAppear 271.0孫子 viewDidAppear 231.0

您可以從此輸出中看到,viewDidLayoutSubviews 保證任何超出視圖控制器主視圖的直接子視圖的內(nèi)容都具有正確的大小.

注意:通常在調(diào)用 viewDidAppear 時,視圖具有正確的大小,但我想說這是絕對不能保證.

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的高度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設(shè)置滾動視圖內(nèi)容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自動滾動)
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)行時,使用 iOS 6.0 SDK 并為 iOS 5 Target 構(gòu)建會導(dǎo)致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創(chuàng)建部分屏幕 UIPageViewController)
UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中縮放和捏合)
主站蜘蛛池模板: 久久91精品国产一区二区 | 在线观看精品 | 91精品国产麻豆 | 日韩有码一区 | 拍拍无遮挡人做人爱视频免费观看 | 伊人超碰 | 91精品久久久久久久久久入口 | 在线视频一区二区 | 色噜噜亚洲男人的天堂 | 亚洲国产精品久久久久秋霞不卡 | 在线成人av | 成人免费视频网站在线观看 | 都市激情亚洲 | 国内精品视频在线观看 | 视频一区二区三区中文字幕 | 久久精品在线 | 亚洲精品中文字幕在线观看 | 久久亚洲精品国产精品紫薇 | 日韩视频在线一区 | 欧美日韩在线播放 | 亚洲欧美日本在线 | 久久精品aaa | 欧美成人第一页 | 亚洲视频手机在线 | 一区二区免费看 | 欧美多人在线 | 久久精品91久久久久久再现 | 国产视频福利一区 | 国产精品久久久久久久久久 | 欧美日韩国产在线观看 | 国产精品精品久久久 | 国产精品久久久久久一区二区三区 | 午夜精品一区二区三区免费视频 | 国产视频第一页 | 亚洲精品中文字幕中文字幕 | 99精品视频一区二区三区 | h在线看 | 亚洲色欲色欲www | 亚洲精品九九 | 中文字幕国产 | 中文字幕电影在线观看 |