将 imageview 限制为 10% 的区域,顶部和前锚之间有空间
Posted
技术标签:
【中文标题】将 imageview 限制为 10% 的区域,顶部和前锚之间有空间【英文标题】:constrain imageview to 10 percent of area with space between top and leading anchor 【发布时间】:2020-05-10 00:00:21 【问题描述】:我想限制我的图像视图与前锚点的间距为 20%,与顶部锚点的间距为 20%。你可以在下面的照片中看到我在寻找什么。不知何故,对于 topAnchor 和leadingAnchor,我们需要考虑 20% 的差距。
var blueMove = UIImageView()
override func viewDidLoad()
super.viewDidLoad()
NSLayoutConstraint.activate([
blueMove.topAnchor.constraint(equalTo: view.topAnchor, constant : 0),
blueMove.heightAnchor.constraint(equalTo: view.heightAnchor, constant : 0),
blueMove.widthAnchor.constraint(equalTo: view.widthAnchor, constant : 0),
blueMove.leadingAnchor.constraint(equalTo: view.leading, constant :0)
])
【问题讨论】:
【参考方案1】:试试
self.blueMove.translatesAutoresizingMaskIntoConstraints = false // don't forget this line
let percent = self.view.frame.width * 0.2 // 20% with 0.2 , 10% with 0.1
NSLayoutConstraint.activate([
blueMove.topAnchor.constraint(equalTo: view.topAnchor, constant :percent),
blueMove.heightAnchor.constraint(equalToConstant:200),
blueMove.widthAnchor.constraint(equalToConstant:200),
blueMove.leadingAnchor.constraint(equalTo: view.leading, constant :percent)
])
【讨论】:
以上是关于将 imageview 限制为 10% 的区域,顶部和前锚之间有空间的主要内容,如果未能解决你的问题,请参考以下文章