来自笔尖的uiview内容是容器外的

时间:2017-05-05 13:19:36

标签: ios uiview nib ibdesignable

我想在自定义UITableViewCell中插入自定义UIView。 我创建了一个带有一些动作的nib文件,我想把这个视图放在我单元格的底部,但内容显示在我单元格的顶部。为什么? enter image description here

编辑:

我的观点代码:

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
}

1 个答案:

答案 0 :(得分:0)

考虑您的观点如下

Consider your view as below

查看代码。上课@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
    }
}

现在在您的单元格中选择您的底部视图,然后在身份检查器中将视图类别更改为此视图,即在这种情况下为BottomView。

enter image description here

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

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