集合视图自动调整大小和设置重复背景
Posted
技术标签:
【中文标题】集合视图自动调整大小和设置重复背景【英文标题】:Collection view auto resizing and setup repeating background 【发布时间】:2020-02-06 07:12:42 【问题描述】:我正在使用集合视图从 API 加载数据。在这里,我想扩展集合视图高度的大小,而不是在集合视图内滚动。并且还需要根据collection view高度重复背景图片。 这是android布局,我想开发类似的。Tap here
导入 UIKit 进口核弹 导入 SVProgressHUD 导入 JSONJoy 类 HomeViewController: UIViewController
@IBOutlet weak var categoryCollection: UICollectionView!
@IBOutlet weak var tabbar: UITabBar!
var sectors:[Sector] = []
var timer = Timer()
var counter = 0
var selectedSector = ""
var selectedSectorName = ""
var webService = ApiService()
let plist = PlistHelper()
override func viewDidLoad()
super.viewDidLoad()
self.categoryCollection.dataSource = self
self.categoryCollection.delegate = self
for item in tabbar.items ?? []
item.image = item.image?.withRenderingMode(.alwaysOriginal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.white], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .selected)
listSectors()
self.categoryCollection.backgroundColor = UIColor(patternImage: UIImage(named: "bg")!)
override func viewWillAppear(_ animated: Bool)
listbanners()
override func viewWillDisappear(_ animated: Bool)
self.timer.invalidate()
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
if(segue.identifier == "sectors")
let vc = segue.destination as! SectorsViewController
vc.sectorCode = selectedSector
vc.sectorName = selectedSectorName
func listSectors()
webService.listSectors( (sectors, message, status) in
if(status)
if let resData = sectors.arrayObject
do
for data in resData
self.sectors.append(try Sector(JSONLoader(data)))
DispatchQueue.main.async
self.categoryCollection.reloadData()
catch let error
print("JSonJoyError:\(error)")
)
扩展 HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
if(collectionView == bannerCollection)
return banners.count
else
return sectors.count
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let options = ImageLoadingOptions(placeholder: UIImage(named: "bannerPlaceholder"),transition: .fadeIn(duration: 0.33))
if(collectionView == bannerCollection)
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! DataCollectionViewCell
Nuke.loadImage(with: URL(string: banners[indexPath.row].ImageUrl ?? "")!, options: options, into:cell.img)
return cell
else
let cell = categoryCollection.dequeueReusableCell(withReuseIdentifier: "catCell", for: indexPath) as! catogeryCollectionViewCell
Nuke.loadImage(with: URL(string: sectors[indexPath.row].ImageUrl ?? "")!, options: options, into:cell.photoImageView)
return cell
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
if(collectionView == categoryCollection)
selectedSector = sectors[indexPath.row].Code ?? "FOOD"
selectedSectorName = sectors[indexPath.row].Name ?? "FOOD"
self.performSegue(withIdentifier: "sectors", sender: self)
扩展 HomeViewController: UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
if(collectionView == bannerCollection)
let size = bannerCollection.frame.size
return CGSize(width: size.width, height: size.height - 10)
else
let size = categoryCollection.frame.size
print("size\(size)")
return CGSize(width: (size.width / 2) - 8, height:120)
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
return 30
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
return 0.0
【问题讨论】:
请详细、恰当地询问。 请添加一些代码和细节。谢谢 对不起.. 我添加了代码。我想在添加新扇区时扩展集合视图的高度。 @neeraj joshi 的回答是对的,我认为你可以试试。 【参考方案1】:以下步骤可以帮助您根据数据增加您的收藏视图高度。
创建高度约束出口。
在主线程中以 0.2 秒的延迟在集合视图中加载数据后, 设置高度约束常量 = 集合视图内容大小高度。
【讨论】:
谢谢。但我得到一个错误。 “无法将类型 'CGSize' 的值分配给类型 'CGFloat'” 重载collectionview后试试这个 请分开行@HaifaMunafar CGFloat height = myCollectionView.collectionViewLayout.collectionViewContentSize.height heightConstraint.constant = height self.view.setNeedsLayout() 或 self.view.layoutIfNeeded()跨度> @HaifaMunafar 内容大小的高度是 CGFloat 本身。以上是关于集合视图自动调整大小和设置重复背景的主要内容,如果未能解决你的问题,请参考以下文章