nib 的 uiview 内容在容器外

Content of uiview from nib is outside container

我想在自定义 UITableViewCell 中插入自定义 UIView。 我创建了一个包含一些操作的 nib 文件,我想将此视图放在我的单元格底部,但内容显示在我的单元格顶部。为什么 ?

编辑:

我的观点代码:

override init(frame: CGRect) {
    super.init(frame: frame)
    xibSetup()
}

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

func xibSetup() {
    view = loadViewFromNib()

    // use bounds not frame or it'll be offset
    view.frame = bounds

    // Make the view stretch with containing view
    view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
    // Adding custom subview on top of our view (over any custom drawing > see note below)
    addSubview(view)
}

func loadViewFromNib() -> UIView {

    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "viewActionsTimeline", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

    return view
}

考虑您的观点如下

查看代码。让你的class@IBDesignable

@IBDesignable
class BottomView: UIView {
    var view: UIView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

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

    func xibSetup() {
        view = loadViewFromNib()

        // use bounds not frame or it'll be offset
        view.frame = bounds

        // Make the view stretch with containing view
        view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]
        // Adding custom subview on top of our view (over any custom drawing > see note below)
        addSubview(view)
    }

    func loadViewFromNib() -> UIView {

        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: "BottomView", bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

        return view
    }
}

现在,在您的单元格 select 您的底部视图中,在 身份检查器中将视图 的 class 更改为此视图,即本例中的 BottomView。

在您的代码中,在单元格的底部视图中添加您的自定义视图

let  bottomView = BottomView(frame: cellBottomView.frame)
cellBottomView.addSubview(bottomView)

我解决了我的问题。 我把这一行:

view.translatesAutoresizingMaskIntoConstraints = true