如何将 UIImageView 放在 tableView 的中心 [英] How do I put an UIImageView in the center of a tableView

查看:33
本文介绍了如何将 UIImageView 放在 tableView 的中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want my image to be in the center in the x axis, I'm writing this code :

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

但它不起作用.

推荐答案

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

As you are trying to create image in code. Try adding anchors between imageView and tableview. Find below peace of example code.

    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 放在 tableView 的中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆