ios 動態化視圖_如何在iOS應用中使高度收集視圖動態化

ios 動態化視圖

by Payal Gupta

通過Payal Gupta

如何在iOS應用中使集合視圖的高度動態化 (How to make height of collection views dynamic in your iOS apps)

充滿活力,就像生活一樣… (Be dynamic, just like life…)

Table views and collection views have always been an integral part of iOS app development. We all might have came across various issues related to them. In this article, we’ll discuss one such problem statement related to collection views.

表格視圖和集合視圖一直是iOS應用程序開發的組成部分。 我們大家可能都遇到過與他們有關的各種問題。 在本文中,我們將討論一種與集合視圖相關的問題陳述。

問題陳述 (Problem Statement)

Let’s assume we’ve a UICollectionView in a UITableViewCell and around 20 UICollectionViewCells that we need to render in it vertically. We can definitely implement that in the blink of an eye with the given data source.

假設我們已經一個UICollectionViewUITableViewCell和周圍20 UICollectionViewCells ,我們需要它垂直于渲染。 我們一定可以在給定數據源的眨眼之間實現它。

Now comes the actual problem statement — we need the UITableViewCell to adjust its height dynamically according to its content. Also, the UICollectionView must be such that all the cells are displayed in one go, i.e. no scrolling allowed.

現在是實際的問題陳述-我們需要UITableViewCell根據其內容動態調整其高度。 同樣, UICollectionView必須確保UICollectionView顯示所有單元格,即不允許滾動。

Long story short: make everything dynamic…

長話短說: 讓一切充滿活力…

讓我們開始編碼 (Let’s start coding)

A view in iOS calculates its height from its content, given there is no height constraint provided. Same goes for UITableViewCell.

鑒于沒有提供高度限制,iOS中的視圖根據其內容計算高度。 UITableViewCell

For now, we’ll keep a single collectionView inside our tableViewCell with its leading, top, trailing and bottom constraints set to 0.

現在,我們將在tableViewCell保留一個collectionView ,其leading, top, trailing and bottom constraints設置為0。

Since we haven’t provided any height constraint to the collectionView and neither do we know the its contentSize beforehand, then how will the tableViewCell calculate its height?

由于我們沒有為collectionView提供任何高度限制,并且我們也不事先知道其contentSize ,那么tableViewCell將如何計算其高度?

(Solution)

Dynamically calculating the collectionView’s height as per its contentSize is simply a 3 step process.

動態計算collectionView's高度按照其contentSize是一個簡單的3個步驟。

1. Subclass UICollectionView and override its layoutSubviews() and intrinsicContentSize , i.e.

1.子類化UICollectionView并覆蓋其layoutSubviews()intrinsicContentSize ,即

The above code will invalidate the intrinsicContentSize and will use the actual contentSize of collectionView. The above code takes into consideration the custom layout as well.

上面的代碼將使intrinsicContentSize無效,并將使用collectionView的實際contentSize 。 上面的代碼也考慮了custom layout

2. Now, set DynamicHeightCollectionView as the collectionView’s class in storyboard.

2.現在,在storyboard中將DynamicHeightCollectionView設置為collectionView's類。

3. One last thing, for the changes to take effect: you need to call layoutIfNeeded() on collectionView, after reloading collectionView’s data, i.e.

3.最后一件事, layoutIfNeeded()更改生效:在重新加載collectionView's數據后,需要在collectionView上調用layoutIfNeeded() ,即

func configure(with arr: [String]) {   self.arr = arr   self.collectionView.reloadData()   self.collectionView.layoutIfNeeded() //Here..!!!}

And there you have it!

在那里,您擁有了!

樣例項目 (Sample Project)

You can download the sample project from here.

您可以從此處下載示例項目。

進一步閱讀 (Further reading)

Don’t forget to read my other articles:

不要忘記閱讀我的其他文章:

  1. Everything about Codable in Swift 4

    Swift 4中有關Codable的一切

  2. Everything you’ve always wanted to know about notifications in iOS

    您一直想了解的有關iOS中通知的所有信息

  3. Deep copy vs. shallow copy — and how you can use them in Swift

    深層副本與淺層副本-以及如何在Swift中使用它們

  4. Coding for iOS 11: How to drag & drop into collections & tables

    iOS 11編碼:如何拖放到集合和表格中

  5. All you need to know about Today Extensions (Widget) in iOS 10

    您需要了解的有關iOS 10中的Today Extensions(Widget)的所有信息

  6. UICollectionViewCell selection made easy..!!

    UICollectionViewCell選擇變得簡單.. !!

Feel free to leave comments in case you have any questions.

如有任何疑問,請隨時發表評論。

翻譯自: https://www.freecodecamp.org/news/how-to-make-height-collection-views-dynamic-in-your-ios-apps-7d6ca94d2212/

ios 動態化視圖

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392163.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392163.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392163.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

新開通博客

新開通博客,希望兄弟們積極更新。 轉載于:https://www.cnblogs.com/ydhliphonedev/archive/2011/07/28/2119720.html

思維導圖分析http之http協議版本

1.結構總覽 在http協議這一章,我將先后介紹上圖六個部分,本文先介紹http的協議版本。 2.http協議版本 http協議的歷史并不長,從1991的0.9版本到現在(2017)僅僅才20多年,算算下來,http還是正處青年,正是大好發展的好時光…

分布與并行計算—生產者消費者模型RabbitMQ(Java)

連接工具 public class ConnectionUtil {public static final String QUEUE_NAME"firstQueue";private static final String RABBIT_HOST "11";private static final String RABBIT_USERNAME "";private static final String RABBIT_PASSWORD…

飛騰 linux 內核,FT2004-Xenomai

移植Xenomai到基于飛騰FT2004 CPU的FT Linux系統1 目前飛騰FT2000/4相關設備驅動還沒有開源,需要先聯系飛騰軟件生態部獲取FT Linux源代碼2 如需在x86交叉編譯arm64內核,推薦使用Linaro gcc編譯器,鏈接如下:https://releases.lina…

使用管道符組合使用命令_如何使用管道的魔力

使用管道符組合使用命令Surely you have heard of pipelines or ETL (Extract Transform Load), or seen some method in a library, or even heard of any tool to create pipelines. However, you aren’t using it yet. So, let me introduce you to the fantastic world of…

關于網頁授權的兩種scope的區別說明

關于網頁授權的兩種scope的區別說明 1、以snsapi_base為scope發起的網頁授權,是用來獲取進入頁面的用戶的openid的,并且是靜默授權并自動跳轉到回調頁的。用戶感知的就是直接進入了回調頁(往往是業務頁面) 2、以snsapi_userinfo為…

安卓流行布局開源庫_如何使用流行度在開源庫之間進行選擇

安卓流行布局開源庫by Ashish Singal通過Ashish Singal 如何使用流行度在開源庫之間進行選擇 (How to choose between open source libraries using popularity) Through my career as a product manager, I’ve worked closely with engineers to build many technology prod…

TCP/IP分析(一) 協議概述

各協議層分工明確 轉載于:https://www.cnblogs.com/HonkerYblogs/p/11247604.html

window 下分linux分區,如何在windows9x下訪問linux分區

1. 簡 介Linux 內 核 支 持 眾 多 的 文 件 系 統 類 型, 目 前 它 可 以 讀 寫( 至 少 是 讀) 大 部 分 的 文 件 系 統.Linux 經 常 與Microsoft Windows 共 存 于 一 個 系 統 或 者 硬 盤 中.Linux 對windows9x/NT 的 文 件 系 統 支 持 的 很 好, 反 之 你 想 在windows 下…

C# new關鍵字和對象類型轉換(雙括號、is操作符、as操作符)

一、new關鍵字 CLR要求所有的對象都通過new來創建,代碼如下: Object objnew Object(); 以下是new操作符做的事情 1、計算類型及其所有基類型(一直到System.Object,雖然它沒有定義自己的實例字段)中定義的所有實例字段需要的字節數.堆上每個對象都需要一些額外的成員,包括“類型…

JDBC01 利用JDBC連接數據庫【不使用數據庫連接池】

目錄: 1 什么是JDBC 2 JDBC主要接口 3 JDBC編程步驟【學渣版本】 5 JDBC編程步驟【學神版本】 6 JDBC編程步驟【學霸版本】 1 什么是JDBC JDBC是JAVA提供的一套標準連接數據庫的接口,規定了連接數據庫的步驟和功能;不同的數據庫提供商提供了一…

leetcode 778. 水位上升的泳池中游泳(并查集)

在一個 N x N 的坐標方格 grid 中,每一個方格的值 grid[i][j] 表示在位置 (i,j) 的平臺高度。 現在開始下雨了。當時間為 t 時,此時雨水導致水池中任意位置的水位為 t 。你可以從一個平臺游向四周相鄰的任意一個平臺,但是前提是此時水位必須…

2020年十大幣預測_2020年十大商業智能工具

2020年十大幣預測In the rapidly growing world of today, when technology is expanding at a rate like never before, there are plenty of tools and skills to explore, learn, and master. In this digital and data age, Business Information and Intelligence have cl…

如何使用MySQL和JPA使用Spring Boot構建Rest API

Hi Everyone! For the past year, I have been learning JavaScript for full-stack web development. For a change, I started to master Java — the powerful Object Oriented Language.嗨,大家好! 在過去的一年中,我一直在學習用于全棧W…

翻譯

令 $m>n>1$ 為正整數. 一個集合含有 $m$ 個給定的實數. 我們從中選取任意 $n$ 個數, 記作 $a_1$, $a_2$, $\dotsc$, $a_n$, 并提問: 是否 $a_1<a_2<\dotsb < a_n$ 正確? 證明: 我們可以最多問 $n!-n^22n-2m(n-1)(1\lfloor \log_{n} m \rfloor)-m$ 個問題&#…

720 智能硬件與 LeanCloud 云端的默契協作

【 玩轉 LeanCloud 】開發者經驗分享&#xff1a; 作者&#xff1a;謝子超 720技術負責人&#xff0c;從業十余年&#xff0c;一直負責軟件開發工作。 我們的產品是與監控和改善室內空氣質量相關的智能硬件&#xff0c;我們使用 LeanCloud 平臺已經有 2 年多了&#xff0c;借此…

linux cifs windows 慢,windows上使用dockerIO特別慢有沒有更優的解決方案?

復制一個大佬的回答Docker for Windows是在Hyper-V虛擬機上跑Linux&#xff0c;文件掛載是通過SMB協議從Windows掛載到Linux&#xff0c;文件讀寫都經過網絡&#xff0c;遇到Laravel這種每次啟動就要加載幾百個文件的框架&#xff0c;文件性能問題就尤其明顯。最好的驗證方法就…

編譯原理—詞法分析器(Java)

1.當運行程序時&#xff0c;程序會讀取項目下的program.txt文件 2. 程序將會逐行讀取program.txt中的源程序&#xff0c;進行詞法分析&#xff0c;并將分析的結果輸出。 3. 如果發現錯誤&#xff0c;程序將會中止讀取文件進行分析&#xff0c;并輸出錯誤提示 所用單詞的構詞規…

【BZOJ4653】[Noi2016]區間 雙指針法+線段樹

【BZOJ4653】[Noi2016]區間 Description 在數軸上有 n個閉區間 [l1,r1],[l2,r2],...,[ln,rn]。現在要從中選出 m 個區間&#xff0c;使得這 m個區間共同包含至少一個位置。換句話說&#xff0c;就是使得存在一個 x&#xff0c;使得對于每一個被選中的區間 [li,ri]&#xff0c;都…

為什么我們需要使用Pandas新字符串Dtype代替文本數據對象

We have to represent every bit of data in numerical values to be processed and analyzed by machine learning and deep learning models. However, strings do not usually come in a nice and clean format and require a lot preprocessing.我們必須以數值表示數據的每…