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

具有動態大小內容的 UIScrollView

UIScrollView with dynamically sized content(具有動態大小內容的 UIScrollView)
本文介紹了具有動態大小內容的 UIScrollView的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

(Xcode 11,斯威夫特)

(Xcode 11, Swift)

作為 iOS 和 Autolayout 的新手,我正在努力實現一個相當簡單的 (恕我直言) 視圖,該視圖顯示 [垂直] 項目列表.唯一的問題是項目是動態決定的,每個項目都可以是文本或圖像(其中任何一個都可能相當大,因此需要滾動).WebView 不是一個選項,因此必須在本地實現.

Being a newbie to iOS and Autolayout, I'm struggling with implementing a fairly simple (IMHO) view which displays a [vertical] list of items. The only problem is that items are decided dynamically and each of them could be either text or image (where either of those could be fairly large so scrolling would be required). WebView is not an option, so it has to be implemented natively.

我是這樣理解這個過程的:

This is how I understand the process:

  • 在 IB 中創建一個 UIScrollView 并將其調整為外框的大小.
  • 將容器視圖作為 UIScrollView 的子視圖(同樣,在 IB 中)并將其大小設置為相同.
  • 設置兩個寬度相等的約束
  • 在運行時,使用 UILabels/UIImageViews 填充容器視圖,并以編程方式設置約束以確保正確布局.
  • 告訴"滾動視圖關于子視圖的高度,以使其管理其滾動.

這是正確的方法嗎?它似乎對我不起作用(對于一個將非常高的圖像動態添加到容器視圖的玩具示例 - 我無法讓滾動工作).在上述過程中執行最后一步的正確方法是什么 - 只需將滾動視圖的 contentSize 強制為填充容器視圖的大小(它似乎對我不起作用).任何幫助將不勝感激.

Is this the right approach? It doesn't seem to work for me (for a toy example of dynamically adding a very tall image to a container view - I cannot get the scrolling to work). What would be the proper way to do the last step in the process above - just force the contentSize of the scrollview to the size of the populated container view (it doesn't seem to work for me). Any help would be appreciated.

推薦答案

在運行時向滾動視圖添加多個元素時,您可能會發現使用 UIStackView 更容易...如果設置正確,它會隨著每個添加的對象自動增加高度.

When adding multiple elements to a scroll view at run-time, you may find it much easier to use a UIStackView... when setup properly, it will automatically grow in height with each added object.

舉個簡單的例子……

1) 首先添加一個 UIScrollView (我給它一個藍色背景以便于查看).將其在所有 4 個方面都限制為零:

1) Start by adding a UIScrollView (I gave it a blue background to make it easier to see). Constrain it to Zero on all 4 sides:

請注意,我們看到紅色圓圈"表示缺少/沖突的約束.暫時忽略它.

Note that we see the "red circle" indicating missing / conflicting constraints. Ignore that for now.

2) 在滾動視圖中添加一個 UIView 作為內容視圖"(我給它一個 systemYellow 背景以便于查看).在內容布局指南的所有 4 個方面將其限制為零——這將(最終)定義滾動視圖的內容大小.還要將其限制為等寬和等高框架布局指南:

2) Add a UIView as a "content view" to the scroll view (I gave it a systemYellow background to make it easier to see). Constrain it to Zero on all 4 sides to the Content Layout Guide -- this will (eventually) define the scroll view's content size. Also constrain it equal width and equal height to the Frame Layout Guide:

重要步驟: 選擇高度約束,然后在 Size Inspector 窗格中選擇 Placeholder - Remove at build time 復選框.這將在設計時滿足 IB 中的自動布局,但將允許該視圖的高度根據需要縮小/增長.

Important Step: Select the Height constraint, and in the Size Inspector pane select the Placeholder - Remove at build time checkbox. This will satisfy auto-layout in IB during design time, but will allow the height of that view to shrink / grow as necessary.

3) 添加一個垂直的 UIStackView 到內容視圖".將其在所有 4 個面上都約束為零.將其屬性配置為Fill/Fill/8(如下圖):

3) Add a Vertical UIStackView to the "content view". Constrain it to Zero on all 4 sides. Configure its properties to Fill / Fill / 8 (as shown below):

4) 在視圖控制器類中添加一個 @IBOutlet 連接到堆棧視圖.現在,在運行時,當您將 UI 元素添加到堆棧視圖時,您的所有可滾動性"都將由自動布局處理.

4) Add an @IBOutlet connection to the stack view in your view controller class. Now, at run-time, as you add UI elements to the stack view, all of your "scrollability" will be handled by auto-layout.

這是一個示例類:

class DynaScrollViewController: UIViewController {

    @IBOutlet var theStackView: UIStackView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // local var so we can reuse it
        var theLabel = UILabel()
        var theImageView = UIImageView()

        // create a new label
        theLabel = UILabel()
        // this gets set to false when the label is added to a stack view,
        // but good to get in the habit of setting it
        theLabel.translatesAutoresizingMaskIntoConstraints = false
        // multi-line
        theLabel.numberOfLines = 0
        // cyan background to make it easy to see
        theLabel.backgroundColor = .cyan
        // add 9 lines of text to the label
        theLabel.text = (1...9).map({ "Line ($0)" }).joined(separator: "
")

        // add it to the stack view
        theStackView.addArrangedSubview(theLabel)

        // add another label
        theLabel = UILabel()
        // multi-line
        theLabel.numberOfLines = 0
        // yellow background to make it easy to see
        theLabel.backgroundColor = .yellow
        // add 5 lines of text to the label
        theLabel.text = (1...5).map({ "Line ($0)" }).joined(separator: "
")

        // add it to the stack view
        theStackView.addArrangedSubview(theLabel)

        // create a new UIImageView
        theImageView = UIImageView()
        // this gets set to false when the label is added to a stack view,
        // but good to get in the habit of setting it
        theImageView.translatesAutoresizingMaskIntoConstraints = false
        // load an image for it - I have one named background
        if let img = UIImage(named: "background") {
            theImageView.image = img
        }
        // let's give the image view a 4:3 width:height ratio
        theImageView.widthAnchor.constraint(equalTo: theImageView.heightAnchor, multiplier: 4.0/3.0).isActive = true

        // add it to the stack view
        theStackView.addArrangedSubview(theImageView)

        // add another label
        theLabel = UILabel()
        // multi-line
        theLabel.numberOfLines = 0
        // yellow background to make it easy to see
        theLabel.backgroundColor = .green
        // add 2 lines of text to the label
        theLabel.text = (1...2).map({ "Line ($0)" }).joined(separator: "
")

        // add it to the stack view
        theStackView.addArrangedSubview(theLabel)

        // add another UIImageView
        theImageView = UIImageView()
        // this gets set to false when the label is added to a stack view,
        // but good to get in the habit of setting it
        theImageView.translatesAutoresizingMaskIntoConstraints = false
        // load a different image for it - I have one named AquariumBG
        if let img = UIImage(named: "AquariumBG") {
            theImageView.image = img
        }
        // let's give this image view a 1:1 width:height ratio
        theImageView.heightAnchor.constraint(equalTo: theImageView.widthAnchor, multiplier: 1.0).isActive = true

        // add it to the stack view
        theStackView.addArrangedSubview(theImageView)

    }

}

如果已遵循這些步驟,您應該會得到以下輸出:

If the steps have been followed, you should get this output:

并且,在滾動到底部之后:

and, after scrolling to the bottom:

這篇關于具有動態大小內容的 UIScrollView的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

iOS - Using storyboard and autolayout to center the UIScrollView(iOS - 使用故事板和自動布局使 UIScrollView 居中)
get index or tag value from imageview tap gesture(從 imageview 點擊手勢獲取索引或標簽值)
UIScrollView not scrolling regardless of large contentSize(無論內容大小如何,UIScrollView 都不會滾動)
UIScrollView zooming with Auto Layout(UIScrollView 使用自動布局縮放)
How to create an image from a UIView / UIScrollView(如何從 UIView/UIScrollView 創建圖像)
iOS/Swift - Hide/Show UITabBarController when scrolling down/up(iOS/Swift - 向下/向上滾動時隱藏/顯示 UITabBarController)
主站蜘蛛池模板: www.一区二区三区 | 亚洲欧美日韩激情 | 亚洲 一区 | 久久综合久久综合久久综合 | 亚洲日本欧美日韩高观看 | 亚洲一卡二卡 | 久久国产精品久久 | 99精品国产一区二区青青牛奶 | 日本亚洲一区 | 精品一区二区三区在线观看 | 国产极品91| 中文字幕91 | av播播 | 高清国产午夜精品久久久久久 | av资源网站 | 在线免费黄色 | 羞羞视频在线免费 | 中文视频在线 | 99精品在线 | 蜜桃av一区二区三区 | 成人在线视频网站 | 成人在线视频网 | 午夜看片| 亚洲一区二区三区免费在线观看 | 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 成人精品一区二区三区中文字幕 | 激情欧美日韩一区二区 | 欧美xxxx性| 日韩有码一区 | 日韩第一夜| 国产精品欧美一区二区三区 | 伊人网伊人网 | 亚洲国产精品一区 | 国产91亚洲精品一区二区三区 | 久久久久久久久久久福利观看 | 国产精品视频观看 | 91九色视频在线 | 一区二区三区在线播放视频 | 91传媒在线播放 | 国产视频一区在线 | 国产免费一区二区三区 |