繁体   English   中英

如何将UIImageView放在tableView的中心

[英]How do I put an UIImageView in the center of a tableView

我希望我的图像位于x轴的中心,我正在编写以下代码:

let emptyImage=UIImageView(frame: CGRect(x: (UIScreen.main.bounds.width)/2 ,y: 200 , width: 50, height: 50))

但这不起作用。

尝试这个

let emptyImage = UIImageView(frame: CGRect(x: UIScreen.main.bounds.width/2 - 50/2, y: 200, width: 50, height : 50)

尝试将imageView center属性设置为tableView.center:

imageView.center = tableView.center

当您尝试在代码中创建图像时。 尝试在imageView和tableview之间添加锚点。 在下面找到示例代码。

    let emptyImage = UIImageView(image: UIImage(named: ""))
    view1.backgroundColor = UIColor.black // view1 consider this as your tableview
    emptyImage.translatesAutoresizingMaskIntoConstraints = false //missed it in first place
    self.view1.addSubview(emptyImage)
    NSLayoutConstraint.activate([
            emptyImage.centerXAnchor.constraint(equalTo: view1.centerXAnchor),
            emptyImage.centerYAnchor.constraint(equalTo: view1.centerYAnchor),
            emptyImage.heightAnchor.constraint(equalToConstant: 100),
            emptyImage.widthAnchor.constraint(equalToConstant: 100)
        ])

希望这可以帮助!

如果要显示UIImageView而不是单元格,请尝试以下操作:

  let emptyImage=UIImageView(frame: 
                             CGRect(x: 0, 
                                    y: 0, 
                          width:   self.tableView.bounds.size.width,            
                          height: self.tableView.bounds.size.height))

  self.tableView.backgroundView = emptyImage

暂无
暂无

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

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