【问题标题】:Why is Child Table View not sizing properly?为什么子表视图大小不正确?
【发布时间】:2020-08-10 18:46:02
【问题描述】:

首先,请不要提出建议我将 TableViewController 作为子视图删除的“聪明”解决方案。谢谢。

总结

我正在以编程方式添加一个 Tableviewcontroller,作为一个固定大小为 216 的视图的子视图。我一直在搞乱约束......并使用 View Hierachy Debugger,我看到 TableView 的高度始终为 852 ...这基本上是屏幕的全尺寸。如何将 TableView 正确调整为其包含视图的大小?

enter image description here

下面是一堆我试图用来约束事物的代码......无济于事。谢谢。

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var xyz: UIView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let child = UITableViewController()
        xyz.addSubview(child.view)
        self.addChild(child)
        child.didMove(toParent: self)
        
        //child.view.translatesAutoresizingMaskIntoConstraints = false
        let safeArea = xyz.layoutMarginsGuide

        
        var height = child.view.heightAnchor.constraint(equalToConstant: 292)
        height = height.constraintWithMultiplier(2000)
        height.isActive = true
        
        
        child.view.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
        child.view.bottomAnchor.constraint(equalTo: safeArea.bottomAnchor).isActive = true
        
        child.view.leftAnchor.constraint(equalTo: safeArea.leftAnchor).isActive = true
        child.view.rightAnchor.constraint(equalTo: safeArea.rightAnchor).isActive = true
    }
    
}

extension NSLayoutConstraint {
    func constraintWithMultiplier(_ multiplier: CGFloat) -> NSLayoutConstraint {
        return NSLayoutConstraint(item: self.firstItem!, attribute: self.firstAttribute, relatedBy: self.relation, toItem: self.secondItem, attribute: self.secondAttribute, multiplier: multiplier, constant: self.constant)
    }
}

【问题讨论】:

  • 你应该取消注释这行child.view.translatesAutoresizingMaskIntoConstraints = false
  • 正确!提交答案。
  • 完成,先生...谢谢

标签: ios swift uitableview constraints nslayoutconstraint


【解决方案1】:

取消注释这行代码

child.view.translatesAutoresizingMaskIntoConstraints = false

【讨论】:

    猜你喜欢
    • 2016-05-22
    • 1970-01-01
    • 2019-10-20
    • 2013-04-16
    • 1970-01-01
    • 2010-11-26
    • 2018-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多