我想知道对此设置约束的正确方法。我在表格 View 单元格内有一个 Collection View ,作为在用户发布的图像之间滑动的一种方式。每个帖子(表格 View 单元格)可以包含多个图像(每个图像都发布在 Collection View 中)。我想要一种类似于 instagram 的风格,你可以在其中滑动图像。我遇到的问题是,当我对 Collection View 和 ImageView 设置约束时,它们不会在设备之间改变。似乎唯一的方法是根据用户使用的设备手动更改 Collection View 单元格和 ImageView 的大小。任何实现我想要实现的外观的替代方法也将受到赞赏。
这里是collection view的约束设置
这是帖子在 iPhone SE 上的样子
Interface Builder 中 Storyboard 上的整体帖子。
这是在图像上设置的约束,它位于 Collection View 内。
最佳答案
第 1 步:创建一个包含 Collection View 的 UITableviewCell(就像 InstagramViewCell)。
import UIKit
class InstagramViewCell: UITableViewCell {
@IBOutlet weak var innerCellView:innerCell!
@IBOutlet weak var collectionView: UICollectionView!
var totalElements = 0
override func awakeFromNib() {
super.awakeFromNib()
let flowLayout = UICollectionViewFlowLayout.init()
flowLayout.itemSize = CGSize.init(width: self.frame.size.width, height: self.frame.size.height)
flowLayout.scrollDirection = .horizontal
self.collectionView?.collectionViewLayout = flowLayout
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.register(UINib.init(nibName: "ImageCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ImageCollectionViewCell")
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func setInformation(_ numberOFCell : Int, _ information : NSDictionary) {
self.totalElements = numberOFCell
self.collectionView.reloadData()
}
}
extension InstagramViewCell:UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize.init(width: self.frame.size.width, height: self.frame.size.height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.totalElements
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as? ImageCollectionViewCell
cell?.setInformation("image")
return cell!
}
}
其中 Collection View 具有以下约束:
第 2 步:创建一个包含 ImageView 的 UICollectionViewCell(类似于 ImageCollectionViewCell)。
import UIKit
class ImageCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
func setInformation(_ imageName : String) {
self.imageView.image = UIImage.init(named: imageName)
}
}
第 3 步:在您的 Controller 中使用 InstagramViewCell。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let backButton = UIButton(type: .custom)
backButton.frame = CGRect(x:0,y:0,width: 45, height:45)
backButton.setImage(UIImage(named: "back-arrow-white"), for: .normal)
let backItem = UIBarButtonItem.init(customView: backButton)
self.navigationItem.leftBarButtonItem = backItem;
table.delegate = self
table.dataSource = self
table.estimatedRowHeight = 100.0
table.rowHeight = UITableViewAutomaticDimension
table.register(UINib.init(nibName: "InstagramViewCell", bundle: nil), forCellReuseIdentifier: "InstagramViewCell")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension ViewController:UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "InstagramViewCell") as? InstagramViewCell
let information:NSDictionary = [:]
cell?.setInformation(10, information)
return cell!
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension;
}
}
关于ios - 在表格 View 单元格内设置 Collection View 约束的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949166/