Vij*_*age 5 constraints ios autolayout swift
我正在屏幕上工作,我想让这个配置文件视图循环。但是我没有从中获得高度或宽度imageView.frame.height以在layer.cornerRadius. 请帮我。谢谢。
这是代码供参考。
private func addImageView() {
topView.addSubview(profilePicView)
NSLayoutConstraint.activate([
profilePicView.centerXAnchor.constraint(equalTo: topView.centerXAnchor),
profilePicView.widthAnchor.constraint(equalTo: topView.heightAnchor, multiplier: 0.3)
])
profilePicViewTopAnchor = profilePicView.topAnchor.constraint(equalTo: view.topAnchor, constant: 64)
profilePicViewTopAnchor?.isActive = true
profilePicViewHeightAnchor = profilePicView.heightAnchor.constraint(equalTo: topView.heightAnchor, multiplier: 0.3)
profilePicViewHeightAnchor?.isActive = true
}
Run Code Online (Sandbox Code Playgroud)
我试图获得价值,
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print((profilePicViewHeightAnchor?.constant)!)
profilePicView.layer.cornerRadius = (profilePicViewHeightAnchor?.constant)! / 2
profilePicView.clipsToBounds = true
}
Run Code Online (Sandbox Code Playgroud)
解决了
在大家的帮助下,我得到了这个对我来说非常适合的解决方案,
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let radius = redView.frame.height * 0.3
profilePicView.layer.cornerRadius = radius / 2
profilePicView.clipsToBounds = true
self.view.layoutIfNeeded()
}
Run Code Online (Sandbox Code Playgroud)
感谢你们所有人。真的很感谢帮助。
这一定是因为在您访问framefor 时imageView,布局约束不是由autolayout. 所以,如果你imageView在一个UIViewController类中,那么你应该override使用下面的方法,然后访问widthand height。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(imageView.frame.height)
}
Run Code Online (Sandbox Code Playgroud)
如果imageView在自定义视图中,那么您可以override使用以下方法,然后访问frame,
override func layoutSubviews() {
super.layoutSubviews()
print(imageView.frame.height)
}
Run Code Online (Sandbox Code Playgroud)
小智 0
请尝试这个对你有帮助。
imageView.layer.cornerRadius = imageView.frame.size.height / 2
imageView.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
确保 imageView 的高度和宽度相同。谢谢
| 归档时间: |
|
| 查看次数: |
1518 次 |
| 最近记录: |