【发布时间】:2019-04-27 19:31:17
【问题描述】:
我正在屏幕上工作,我想让这个配置文件视图循环。但我没有从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
}
我正在尝试获取值,
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print((profilePicViewHeightAnchor?.constant)!)
profilePicView.layer.cornerRadius = (profilePicViewHeightAnchor?.constant)! / 2
profilePicView.clipsToBounds = true
}
已解决
在大家的帮助下,我得到了这个非常适合我的解决方案,
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let radius = redView.frame.height * 0.3
profilePicView.layer.cornerRadius = radius / 2
profilePicView.clipsToBounds = true
self.view.layoutIfNeeded()
}
谢谢大家。非常感谢您的帮助。
【问题讨论】:
-
你在使用 Storyboard 吗?
-
你试过
imageviewHeightConstraint.constant吗? -
请在问题中添加相关代码。
-
@iDev750 不,我正在以编程方式使用自动布局
-
profilePicViewHeightAnchor.constant将始终为零。您应该在viewDidLayoutSubviews中以imageView.frame访问profilePicView中的frame,并在activate方法中移动那些top和heightanchors并删除类变量(即profilePicViewTopAnchor&profilePicViewHeightAnchor)
标签: ios swift autolayout constraints