Ios 来自nib的uiview内容位于容器外部
我想在自定义UITableViewCell中插入自定义UIView。 我已经创建了一个带有一些操作的nib文件,我想把这个视图放在单元格的底部,但是内容显示在单元格的顶部。为什么? 编辑: 我的看法是:Ios 来自nib的uiview内容位于容器外部,ios,uiview,nib,ibdesignable,Ios,Uiview,Nib,Ibdesignable,我想在自定义UITableViewCell中插入自定义UIView。 我已经创建了一个带有一些操作的nib文件,我想把这个视图放在单元格的底部,但是内容显示在单元格的顶部。为什么? 编辑: 我的看法是: override init(frame: CGRect) { super.init(frame: frame) xibSetup() } required init?(coder aDecoder: NSCoder) { super.init(coder: aDeco
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
}
考虑你的观点如下 查看代码。使您的类
@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
}
}
现在在单元格中选择底部视图,并在身份检查器中将视图类更改为该视图,即本例中的底部视图
或
在代码中,在单元格的底部视图中添加自定义视图
let bottomView = BottomView(frame: cellBottomView.frame)
cellBottomView.addSubview(bottomView)
我解决了我的问题。
我这样说:
view.translatesAutoresizingMaskIntoConstraints = true
您是如何尝试将视图从nib文件放入原型单元的?可能是因为您没有指定其框架/约束。约束很好。我已经把我的代码放在我的问题里了。我真的想让视图独立起来,这样如果我添加一个按钮,它就会同时添加到我的所有单元格中。我认为这个解决方案会对你有所帮助。