ios - 如何将具有动态高度的 xib subview 添加到 UIView

标签 ios swift uiview xib ios-autolayout

这应该很容易做到,而且在 android 中非常容易(使用 LinearLayout),但我不知道如何在 ios 中做到这一点。

我有 UIView,我想在其中添加一些动态高度 subview 。 subview 从 xib 文件加载并包含 2 个具有动态高度的 UILabel。

class Subview: UIView {

    @IBOutlet var contentView: UIView!

    @IBOutlet weak var title: UILabel!
    @IBOutlet weak var desc: UILabel!


    override init(frame: CGRect) {
        super.init(frame: frame)
        self.commonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.commonInit()
    }

    fileprivate func commonInit() {
        Bundle.main.loadNibNamed("Subview", owner: self, options: nil)
        addSubview(contentView)
        self.contentView.frame = self.bounds
        self.contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }

}

subview 是这样添加的

var containerView: UIView

func setupView() {

    let subview1 = Subview()
    subview1.title = "Some title"
    subview1.desc = "Some very long description"

    self.containerView.addSubview(subview1)

}

问题是在这种情况下,不显示 subview 。 如果我添加

subview1.frame = CGSize(width: self.bounds.width, height: 100)

显示了 subview ,但高度 100 不正确

如果我尝试计算 subview 大小

let measuredSize = subview1.sizeThatFits(CGSize(width: self.bounds.width, height: CGFloat(MAXFLOAT)))

测量的宽度和尺寸为 0。

知道我在这里做错了什么吗?谢谢!

编辑:这是我的带有约束的 subview xib 文件 enter image description here

最佳答案

您可以通过以下方式轻松完成此操作

1- 在 xib 文件中从上到下正确 Hook 约束

2- theView.translatesAutoresizingMaskIntoConstraints = false

3-设置前导、尾随、顶部约束(无高度),例如,这是一个带有一个标签的简单 xib enter image description here

添加到self.view by时

let vv = (Bundle.main.loadNibNamed("repairView", owner: self, options: nil))?[0] as! repairView;

vv.translatesAutoresizingMaskIntoConstraints = false

vv.aedlb.text = "dsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbd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dsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsvvvvdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsvvvvdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjdsdsjhdsjhdsjhsdhjdsjhesisbeudjbdsjbdsjds"

view.addSubview(vv)

NSLayoutConstraint.activate([

    vv.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),
    vv.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0),
    vv.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0),

    ])

结果 enter image description here

关于ios - 如何将具有动态高度的 xib subview 添加到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51327195/

相关文章:

ios - 如何快速创建静态类?

ios - 所有 UIView 过渡动画停止工作

ios - Swift:运行时:线程问题:MyApp.DownloadManager.progress.setter 中的数据竞争:Swift.Float at 0x7b1400209130

ios - 加速度计、陀螺仪和磁力计

ios - 存在 tabBar 时出现奇怪的定位问题

ios - 是否可以从 iTunes 中列出应用程序中使用的库

android - 汇编程序可以在 OS X、iOS 和 Android 上运行吗?

ios - Swift tableview在删除单元格过程indexPath.row问题

ios - UIView 在 UIScrollView 中有上边距

iphone - 显示 UITableView 溢出?