<bdo id='wYfHY'></bdo><ul id='wYfHY'></ul>
    <legend id='wYfHY'><style id='wYfHY'><dir id='wYfHY'><q id='wYfHY'></q></dir></style></legend>
  • <i id='wYfHY'><tr id='wYfHY'><dt id='wYfHY'><q id='wYfHY'><span id='wYfHY'><b id='wYfHY'><form id='wYfHY'><ins id='wYfHY'></ins><ul id='wYfHY'></ul><sub id='wYfHY'></sub></form><legend id='wYfHY'></legend><bdo id='wYfHY'><pre id='wYfHY'><center id='wYfHY'></center></pre></bdo></b><th id='wYfHY'></th></span></q></dt></tr></i><div id='wYfHY'><tfoot id='wYfHY'></tfoot><dl id='wYfHY'><fieldset id='wYfHY'></fieldset></dl></div>
  • <tfoot id='wYfHY'></tfoot>

      <small id='wYfHY'></small><noframes id='wYfHY'>

      1. 在 UITableViewCell 中加载的 Nib 文件 UIView 不会拉伸

        时间:2023-09-09

        • <legend id='gVrbl'><style id='gVrbl'><dir id='gVrbl'><q id='gVrbl'></q></dir></style></legend>

          <tfoot id='gVrbl'></tfoot>

            <tbody id='gVrbl'></tbody>
          • <bdo id='gVrbl'></bdo><ul id='gVrbl'></ul>

                <small id='gVrbl'></small><noframes id='gVrbl'>

                <i id='gVrbl'><tr id='gVrbl'><dt id='gVrbl'><q id='gVrbl'><span id='gVrbl'><b id='gVrbl'><form id='gVrbl'><ins id='gVrbl'></ins><ul id='gVrbl'></ul><sub id='gVrbl'></sub></form><legend id='gVrbl'></legend><bdo id='gVrbl'><pre id='gVrbl'><center id='gVrbl'></center></pre></bdo></b><th id='gVrbl'></th></span></q></dt></tr></i><div id='gVrbl'><tfoot id='gVrbl'></tfoot><dl id='gVrbl'><fieldset id='gVrbl'></fieldset></dl></div>
                1. 本文介绍了在 UITableViewCell 中加载的 Nib 文件 UIView 不会拉伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  I have a UIView which is reusable via a nib/xib-file. I want to load this and fill a UITableViewCell which is to be used in a self-resizing UITableView. All with auto-layout.

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

                  Most works good, but It seems as the loaded UIView uses the added constraints around it to shrink the UITableViewCell's contentView. This is good for the height, but I don't want this for the width.

                  忽略下面的灰色单元格,这只是一个选定的单元格.

                  Ignore the grey cell below, this is just a selected cell.

                  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 以及它的文件所有者.两者都有效".

                  In RowView0002.xib I have set the rootviews background to red, added a UILabel with 4 constraints to it's sides with some margin as you can see. I both tried to set the rootView to the class RowView as well as it's File's Owner. Both "works".

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

                  Any idea how to get the contentView to match the UITableView?

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

                  *Edit 1: The green is the background of the UILabel. The red is the background of the nib-file. After running the app the View heirarcy is: UITableViewCell > ContentView > RowView > NibFileView (red) > UILabel (green)

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

                  Inspecting the view hierarchy shows that all constraints is setup as expected. However the UITableViewContentView have constraints that match the total size seen (wrong):

                  self.width = 156.5 @ 1000
                  

                  推荐答案

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

                  I solved it by also adding a width constraint matching the tableViews width. This is code from CustomTableViewCell:

                  public override func layoutSubviews() {
                      super.layoutSubviews()
                  
                      if let width = tableView()?.frame.width, !haveAddedWidthConstraint {
                          haveAddedWidthConstraint = true
                          rowView.addWidthConstraint(width: width)
                      }
                  }
                  

                  UIViewExtension:

                  UIViewExtension:

                  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:

                  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
                  }
                  

                  这篇关于在 UITableViewCell 中加载的 Nib 文件 UIView 不会拉伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:带图像的动态 UITableView 下一篇:以编程方式对搜索栏和 UITableView 使用 AutoLayout

                  相关文章

                2. <i id='Gm6JU'><tr id='Gm6JU'><dt id='Gm6JU'><q id='Gm6JU'><span id='Gm6JU'><b id='Gm6JU'><form id='Gm6JU'><ins id='Gm6JU'></ins><ul id='Gm6JU'></ul><sub id='Gm6JU'></sub></form><legend id='Gm6JU'></legend><bdo id='Gm6JU'><pre id='Gm6JU'><center id='Gm6JU'></center></pre></bdo></b><th id='Gm6JU'></th></span></q></dt></tr></i><div id='Gm6JU'><tfoot id='Gm6JU'></tfoot><dl id='Gm6JU'><fieldset id='Gm6JU'></fieldset></dl></div>
                  <tfoot id='Gm6JU'></tfoot>

                    <bdo id='Gm6JU'></bdo><ul id='Gm6JU'></ul>
                3. <small id='Gm6JU'></small><noframes id='Gm6JU'>

                    <legend id='Gm6JU'><style id='Gm6JU'><dir id='Gm6JU'><q id='Gm6JU'></q></dir></style></legend>