简体   繁体   English

使用swift以编程方式向从父类继承的子类的uitableview添加前导和尾随空格

[英]Adding leading and trailing spaces to uitableview of child class inherited from parent class programmatically using swift

I am trying to add leading and trailing spaces to tableview embedded in view programmatically. 我正在尝试以编程方式将前导和尾随空格添加到嵌入视图的tableview中。 This tableview has constraints been declared in parent class i am trying to override them in child class. 此表视图已在父类中声明了约束,我试图在子类中重写它们。 I am doing in the following way 我正在按照以下方式做

class TestTableView: BaseTableViewController{

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    self.lpBaseTableView.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.lpBaseTableView.contentSize.height)
    self.lpBaseTableView.center = self.view.center
    self.lpBaseTableView.translatesAutoresizingMaskIntoConstraints = false
    self.lpBaseTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16).isActive = true
    self.lpBaseTableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 16).isActive = true

 }
}

After researching few links from stack overflow i am using the above code but there is no change in the table view position as shown below in the image. 在研究了来自堆栈溢出的少量链接后,我使用了上面的代码,但是表格视图位置没有变化,如下图所示。 Please help me out. 请帮帮我。 I am new to swift and iOS 我是Swift和iOS的新手 视图中的TableView

Adding a leading and trailing constraints are simple, just give proper constants with respect to the superview and also give top and bottom constraints. 添加前导约束和尾随约束很简单,仅给出关于超级视图的适当常数,还给出顶部和底部约束。 Frame and centre is not needed since you're using autolayout here. 不需要边框和居中,因为您在此处使用自动布局。

    lpBaseTableView = UITableView()
    lpBaseTableView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(lpBaseTableView)

    NSLayoutConstraint.activate([
        lpBaseTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16),
        lpBaseTableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16),
        lpBaseTableView.topAnchor.constraint(equalTo: view.topAnchor),
        lpBaseTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ]) 

You can try with SnaKit 您可以尝试使用SnaKit

    import SnapKit

    lpBaseTableView.snp.makeConstraints { (make) in
    make.center.equalTo(view)
    make.left.equalTo(view.snp.left)
    make.right.equalTo(view.snp.right)

Please note that if there is also constraints from Storyboard for same object it could not work properly 请注意,如果情节提要还存在针对同一对象的约束,则该约束无法正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM