下面是我的配置文件视图约束,视图呈现得很好,但是宽度始终返回零。因此,最后一个约束profileImageView.layer.cornerRadius = (profile.frame.width / 2)每次都返回零。
profileImageView.translatesAutoresizingMaskIntoConstraints = false
addConstraint(NSLayoutConstraint(item: profileImageView, attribute: .width, relatedBy: .equal, toItem: containerView, attribute: .width, multiplier: 0.125, constant: 0))
addConstraint(NSLayoutConstraint(item: profileImageView, attribute: .height, relatedBy: .equal, toItem: containerView, attribute: .width, multiplier: 0.125, constant: 0))
addConstraint(NSLayoutConstraint(item: profileImageView, attribute: .centerY, relatedBy: .equal, toItem: containerView, attribute: .centerY, multiplier: 1, constant: 0))
addConstraint(NSLayoutConstraint(item: profileImageView, attribute: .centerX, relatedBy: .equal, toItem: containerView, attribute: .centerX, multiplier: 0.25, constant: 0))
profileImageView.layer.cornerRadius = (profileImageView.frame.width / 2)有什么建议吗?

发布于 2017-05-10 19:15:38
profileImageView.frame.width是零,因为它的帧还没有计算。在profileImageView重写layoutSubviews方法中:
override func layoutSubviews() {
super.layoutSubviews()
layer.cornerRadius = bounds.width / 2.0
}或者,如果使用的是视图控制器,请重写viewDidLayoutSubviews方法:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
profileImageView.layer.cornerRadius = profileImageView.bounds.width / 2.0
}发布于 2017-05-10 19:15:34
profileImageView还没有布局,也没有尺寸。假设这是在UIViewController子类中,您可以将角半径代码放在viewDidLayoutSubviews中。
override func viewDidLayoutSubviews() {
profileImageView.layer.cornerRadius = (profileImageView.frame.width / 2)
}https://stackoverflow.com/questions/43900795
复制相似问题