ios - 如何在 Swift 中将 xib 的垂直长度设置为代码

标签 ios swift xcode

我想在代码中灵活修改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/

相关文章:

ios - 在 Xcode 上模拟 Cordova 应用程序时如何更新代码?

swift - 如何在 RealityKit 中为变换设置动画

ios - Twitter 仅应用程序身份验证始终返回 401

xcode - 创建一个到不同的 NavigationController 的 segue

ios - 如何在使用 `layoutAttributesForElements` 时为 Collection View 布局更改设置动画?

ios - Swift:将 [[[CGFloat]]] 合并到 [[CGFloat]] 与额外数据的正确方法是什么

xcode - 在 High Sierra 10.13.6 上将 Xcode 10.1 更新到 10.2

iphone - 无法更新 iPhone 应用程序

ios - 在 iOS 中设置时区的正确格式是什么?

ios - iTunes Connect 中的应用程序图标丢失