layer.cornerRadius 不能与 NSLayoutConstraints 结合使用 (swift 3)
layer.cornerRadius not working in conjunction with NSLayoutConstraints (swift 3)
以下是我的配置文件视图约束,视图呈现良好,但宽度始终 returns 为零。所以最后的约束,profileImageView.layer.cornerRadius = (profile.frame.width / 2) returns 每次都为零。
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)
有什么建议吗?
profileImageView
还没有排版,没有尺码。假设这是在 UIViewController
子类中,您可以将圆角半径代码放在 viewDidLayoutSubviews
:
中
override func viewDidLayoutSubviews() {
profileImageView.layer.cornerRadius = (profileImageView.frame.width / 2)
}
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
}
以下是我的配置文件视图约束,视图呈现良好,但宽度始终 returns 为零。所以最后的约束,profileImageView.layer.cornerRadius = (profile.frame.width / 2) returns 每次都为零。
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)
有什么建议吗?
profileImageView
还没有排版,没有尺码。假设这是在 UIViewController
子类中,您可以将圆角半径代码放在 viewDidLayoutSubviews
:
override func viewDidLayoutSubviews() {
profileImageView.layer.cornerRadius = (profileImageView.frame.width / 2)
}
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
}