私はnib/xibファイル経由で再利用可能なUIViewを持っています。私はこれをロードし、自己サイズ変更のUITableViewで使用されるUITableViewCellを入力します。すべてが自動レイアウトです。NIBファイルロードされたUIViewは伸びません
ほとんどの場合うまくいきますが、ロードされたUIViewは、その周りに追加された制約を使用して、UITableViewCellのcontentViewを縮小するようです。これは高さには良いですが、私は幅のためにこれをしたくありません。
以下の灰色のセルは無視してください。これは選択されたセルに過ぎません。私は赤にrootviewsの背景を設定しているRowView0002.xibで
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cellId = "RowView0002"
var cell = tableView.dequeueReusableCell(withIdentifier: cellId)
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellId)
let subView = RowView(frame: cell!.frame)
cell!.contentView.attachViewWithConstraints(subView)
let _ = subView.viewLoadedFromNibAttached(name: cellId)
}
return cell!
}
override public func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tableView.estimatedRowHeight = 40.0
tableView.rowHeight = UITableViewAutomaticDimension
}
extension UIView
{
public func attachViewWithConstraints(_ view:UIView)
{
addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
view.layoutAttachAll(to: self)
}
@discardableResult
public func viewLoadedFromNibAttached<T : UIView>(name:String) -> T? {
guard let view = Bundle.main.loadNibNamed(name, owner: self, options: nil)?[0] as? T else {
return nil
}
attachViewWithConstraints(view)
return view
}
public func layoutAttachAll(to childView:UIView)
{
var constraints = [NSLayoutConstraint]()
childView.translatesAutoresizingMaskIntoConstraints = false
constraints.append(NSLayoutConstraint(item: childView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: childView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: childView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0))
constraints.append(NSLayoutConstraint(item: childView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0))
childView.addConstraints(constraints)
}
、あなたが見ることができるよう余裕でそれの側面に4つの制約にUILabelを追加しました。私はrootViewクラスをRowViewクラスに設定しようとしましたが、それはFile's Ownerです。両方とも "働く"。
どのようにコンテンツビューを取得するにはどのようなアイデアは、UITableViewに一致する?
*編集1: 緑はUILabelの背景です。赤色は、nibファイルの背景です。アプリケーションを実行すると、次のような情報が表示されます。UITableViewCell> ContentView> RowView> NibFileView(赤色)> UILabel(緑色)
ビュー階層を調べると、すべての制約が期待通りに設定されていることがわかります。 UITableViewContentViewが見合計サイズを一致させる制約(間違った)を持っているしかし:それは自分自身のサイジングですので
それが問題のように見えるself.width = 156.5 @ 1000
にTVCの区切り線があなたの意見の終わりとは異なる高さであるのはなぜ?あなたの視点が占める高さは、セルの高さの3倍です。重複していないと確信していますか?ビューデバッガを使用してディメンションを確認してください。 – Andrea
左側のtableviewは無視してください。別のUITableViewです。 – Sunkas
私は理解していないいくつかのものがあります、グリーンはxibからのあなたの意見ですか?赤はTVCのcontentViewですか? – Andrea