我想在代码中灵活修改Xib Size Inspector中View的高度。
我还想修改 Xib 中 UICollectionView 的 Constraints 中的 Height Equals。
短代码:
class ShopImagesViewCell: UITableViewCell
{
@IBOutlet weak var imageCollectionView: UICollectionView!
override func awakeFromNib()
{
super.awakeFromNib()
// ......
}
}
extension ShopImagesViewCell
{
func setView(urls : [String], isZzip : Bool)
{
self.imageCollectionView.delegate = self
self.imageCollectionView.dataSource = self
self.imageCollectionView.regCells(cells:
["ShopImageCollectionViewCell","ShopNonImageCollectionViewCell"])
// ......
}
}
extension ShopImagesViewCell : UICollectionViewDelegateFlowLayout, UITableViewDelegate
{
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return CGFloat(291) // It does not work.
}
}
首次加载 Xib 时,仅执行一次以适应大小。
代码中修改Xib高度长度,代码中修改UICollectionView中Constraintsd的Height Equals。
请告诉我应该在哪里使用该代码。
最佳答案
根据您粘贴的代码,您似乎将自定义 UITableViewCell
放入 Nib 中。假设,如果没有 UITableViewDelegate
的帮助,您无法直接设置高度。
在UITableViewDelegate
实现的地方或者您打算实现的地方添加此委托(delegate)方法
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// you have the indexPath based on which you can set the height dynamically, based on the cell position.
// return your intended height here
}
希望有帮助。
关于ios - 如何在 Swift 中将 xib 的垂直长度设置为代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53182788/