3

我有一个可通过 nib/xib 文件重用的 UIView。我想加载它并填充一个 UITableViewCell,它将用于自调整大小的 UITableView。全部带有自动布局。

大多数都很好,但似乎加载的 UIView 使用它周围添加的约束来缩小 UITableViewCell 的 contentView。这对高度有好处,但我不希望这个宽度。

UITableViewCell 的外观 忽略下面的灰色单元格,这只是一个选定的单元格。

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)
    }

在 RowView0002.xib 中,我将 rootviews 背景设置为红色,在其两侧添加了一个带有 4 个约束的 UILabel,如您所见。我都尝试将 rootView 设置为类 RowView 以及它的文件所有者。两者都“有效”。

知道如何让 contentView 与 UITableView 匹配吗?

*编辑1:绿色是UILabel的背景。红色是 nib 文件的背景。运行应用程序后,View heirarcy 是: UITableViewCell > ContentView > RowView > NibFileView (red) > UILabel (green)

检查视图层次结构表明所有约束都按预期设置。但是 UITableViewContentView 具有与看到的总大小相匹配的约束(错误):

self.width = 156.5 @ 1000
4

4 回答 4

5

layoutAttachAll 的完整实现如下。

先来一些使用示例:

 // pin all edge to superview
 myView.layoutAttachAll()

 // pin all edges (to superview) with margin:
 myView.layoutAttachAll(margin: 8.0)

 // for child views: pin leading edge to superview's leading edge:
 myView.layoutAttachLeading()

 // for sibling views: pin leading edge to siblingView's trailing edge:
 myView.layoutAttachLeading(to: siblingView)

 // for sibling views: pin top edge to siblingView's bottom edge:
 myView.layoutAttachTop(to: siblingView)

注意:在使用这些方法附加到超级视图之前,必须将 myView 添加为子视图。此外,所有参与视图必须设置为 translatesAutoresizingMaskIntoConstraints = false。

完整的实现:

import UIKit

extension UIView {

    /// attaches all sides of the receiver to its parent view
    func layoutAttachAll(margin : CGFloat = 0.0) {
        let view = superview
        layoutAttachTop(to: view, margin: margin)
        layoutAttachBottom(to: view, margin: margin)
        layoutAttachLeading(to: view, margin: margin)
        layoutAttachTrailing(to: view, margin: margin)
    }

    /// attaches the top of the current view to the given view's top if it's a superview of the current view, or to it's bottom if it's not (assuming this is then a sibling view).
    /// if view is not provided, the current view's super view is used
    @discardableResult
    func layoutAttachTop(to: UIView? = nil, margin : CGFloat = 0.0) -> NSLayoutConstraint {

        let view: UIView? = to ?? superview
        let isSuperview = view == superview
        let constraint = NSLayoutConstraint(item: self, attribute: .top, relatedBy: .equal, toItem: view, attribute: isSuperview ? .top : .bottom, multiplier: 1.0, constant: margin)
        superview?.addConstraint(constraint)

        return constraint
    }

    /// attaches the bottom of the current view to the given view
    @discardableResult
    func layoutAttachBottom(to: UIView? = nil, margin : CGFloat = 0.0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint {

        let view: UIView? = to ?? superview
        let isSuperview = (view == superview) || false
        let constraint = NSLayoutConstraint(item: self, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: isSuperview ? .bottom : .top, multiplier: 1.0, constant: -margin)
        if let priority = priority {
            constraint.priority = priority
        }
        superview?.addConstraint(constraint)

        return constraint
    }

    /// attaches the leading edge of the current view to the given view
    @discardableResult
    func layoutAttachLeading(to: UIView? = nil, margin : CGFloat = 0.0) -> NSLayoutConstraint {

        let view: UIView? = to ?? superview
        let isSuperview = (view == superview) || false
        let constraint = NSLayoutConstraint(item: self, attribute: .leading, relatedBy: .equal, toItem: view, attribute: isSuperview ? .leading : .trailing, multiplier: 1.0, constant: margin)
        superview?.addConstraint(constraint)

        return constraint
    }

    /// attaches the trailing edge of the current view to the given view
    @discardableResult
    func layoutAttachTrailing(to: UIView? = nil, margin : CGFloat = 0.0, priority: UILayoutPriority? = nil) -> NSLayoutConstraint {

        let view: UIView? = to ?? superview
        let isSuperview = (view == superview) || false
        let constraint = NSLayoutConstraint(item: self, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: isSuperview ? .trailing : .leading, multiplier: 1.0, constant: -margin)
        if let priority = priority {
            constraint.priority = priority
        }
        superview?.addConstraint(constraint)

        return constraint
    }
}
于 2017-09-18T12:35:10.203 回答
1

我还通过添加与 tableViews 宽度匹配的宽度约束来解决它。这是来自 CustomTableViewCell 的代码:

public override func layoutSubviews() {
    super.layoutSubviews()

    if let width = tableView()?.frame.width, !haveAddedWidthConstraint {
        haveAddedWidthConstraint = true
        rowView.addWidthConstraint(width: width)
    }
}

UIView 扩展:

public func addWidthConstraint(width: CGFloat) {
    let widthConstraint = NSLayoutConstraint(item: self, attribute: .width, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: width)
    widthConstraint.priority = 1000
    addConstraint(widthConstraint)
}

UITableViewCellExtension:

func tableView() -> UITableView? {
    var currentView: UIView = self
    while let superView = currentView.superview {
        if superView is UITableView {
            return (superView as! UITableView)
        }
        currentView = superView
    }
    return nil
}
于 2017-02-21T14:20:28.980 回答
0

看起来问题在于创建的单元格UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: cellId)没有关于表格视图宽度的信息,因此它根据标签的宽度调整自身大小。显然,表格视图/单元格不会强制单元格的内容视图采用其宽度。

您可能想重新编写一些此单元处理代码。

如果你想从 xib 加载你的单元格,你可以跳过所有有约束的东西。只需执行:

 override func viewDidLoad() {
    //...
    let nib = UINib(nibName: "RowView0002", bundle: NSBundle.main)
    tableView.reigsterNib(nib, forCellReuseIdentifier: "RowView0002")
 }

非常重要:.xib 文件中的第一个顶级项必须是 UITableViewCell。Nibs默认为UIViews,删除IB中的视图,从IB右下角的对象库中拖出一个UITableViewCell。然后,如有必要,将其子类设置为您创建的 UITableViewCell 子类。(您可能还需要在 IB 中设置重用标识符。)

然后在tableView(_:cellForRowAt IndexPath:)

guard let cell = tableView.dequeueResuableCell(withIdentifier: "RowView0002", for: indexPath) as? TheNibUITableViewSubclass else { //something went wrong, probably crash } 
cell.label.text = //...
return cell 

您可能希望将“RowView0002”放在某个常量中。

如果“RowView0002”和RowView类都需要是视图,您可能应该创建一个UITableViewCell. 覆盖只是init(style:resueIdentifier:) and after callingsuper` 在上面的代码中添加您的子视图。希望这可以帮助!

于 2017-02-04T00:37:18.413 回答
-1

我通过在我的自定义UITableViewCell类中公开一个设置 UITableViewCell 的锚约束并从父 UITableView cellForRowAtIndexPath方法调用它的方法来解决它。

这是Objective C中的代码。

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

    YourTableViewCellType *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    **[cell expandToParentSize];** // Self-sizing magic!
    
    return cell;
}

// Write below code in "YourTableViewCellType" class. "containerStack" is my UIStackView that holds all controls in the nib UITableViewCell

(void)expandToParentSize 
{    
    [NSLayoutConstraint activateConstraints:@[
        [self.containerStack.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor],
        [self.containerStack.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor],
        [self.containerStack.topAnchor constraintEqualToAnchor:self.contentView.topAnchor],
        [self.containerStack.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor]
     ]];
}
于 2020-10-21T23:59:51.877 回答