帮助迅速布局(简单的你不是我)

问题描述 投票:0回答:1

嗨,我正在学习如何制作新闻源,虽然事情进展顺利,但我希望在新闻源本身的布局方面得到一些帮助。该feed剪辑到模拟器的安全区域,但我想把顶部部分向下移动约一半的屏幕。我已经用XIB制作了表格视图,并以编程方式创建了表格视图。如何才能将顶部的部分向下移动?下面是代码。

HomeViewScene = UITableView(frame: view.bounds, style: .plain)
    HomeViewScene.backgroundColor = Colors.white


    view.addsubview(tableview)



    let cellNib = UINib(nibName: "PosterCell", bundle: nil)
    HomeViewScene.register(cellNib, forCellReuseIdentifier: "postCell")


    var layoutguide:UILayoutGuide
    layoutguide = view.safeAreaLayoutGuide




    HomeViewScene.leadingAnchor.constraint(equalTo: layoutguide.leadingAnchor).isActive = true
    HomeViewScene.topAnchor.constraint(equalTo: layoutguide.topAnchor).isActive = true
    HomeViewScene.trailingAnchor.constraint(equalTo: layoutguide.trailingAnchor).isActive = true
    HomeViewScene.bottomAnchor.constraint(equalTo: layoutguide.bottomAnchor).isActive = true

What the simulator shows

What I want the top constraint to be attached to (that was done on word)

swift xcode uitableview constraints
1个回答
0
投票

在我看来,你已经很接近了。我建议的2件主要事情是:1) 你需要设置 translatesAutoresizingMaskIntoConstraints 为false,如果你想让你的约束真正生效.2)如果你想让你的表视图在你的另一个视图下,你需要将你的表视图的顶部锚点设置为等于你的另一个视图的底部锚点。

具体来说,我会这样修改你发布的代码来解决你的问题。

HomeViewScene = UITableView(frame: view.bounds, style: .plain)
    HomeViewScene.backgroundColor = Colors.white


    view.addsubview(tableview)



    let cellNib = UINib(nibName: "PosterCell", bundle: nil)
    HomeViewScene.register(cellNib, forCellReuseIdentifier: "postCell")


    var layoutguide:UILayoutGuide
    layoutguide = view.safeAreaLayoutGuide

    HomeViewScene.translatesAutoresizingMaskIntoConstraints = false //Add this line


    HomeViewScene.leadingAnchor.constraint(equalTo: layoutguide.leadingAnchor).isActive = true
    HomeViewScene.topAnchor.constraint(equalTo: otherView.topAnchor).isActive = true //You need to make sure the TOP of the table view is positioned right at the BOTTOM of the other view
    HomeViewScene.trailingAnchor.constraint(equalTo: layoutguide.trailingAnchor).isActive = true
    HomeViewScene.bottomAnchor.constraint(equalTo: layoutguide.bottomAnchor).isActive = true

记住,如果你用编程方式改变一个视图的约束条件,你必须设置为: translatesAutoresizingMaskIntoConstraints 为false,否则你将看到该视图的位置没有变化。

© www.soinside.com 2019 - 2024. All rights reserved.