首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >layer.cornerRadius未与NSLayoutConstraints协同工作( with 3)

layer.cornerRadius未与NSLayoutConstraints协同工作( with 3)
EN

Stack Overflow用户
提问于 2017-05-10 19:04:14
回答 2查看 4.9K关注 0票数 6

下面是我的配置文件视图约束,视图呈现得很好,但是宽度始终返回零。因此,最后一个约束profileImageView.layer.cornerRadius = (profile.frame.width / 2)每次都返回零。

代码语言:javascript
复制
    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)

有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-10 19:15:38

profileImageView.frame.width是零,因为它的帧还没有计算。在profileImageView重写layoutSubviews方法中:

代码语言:javascript
复制
override func layoutSubviews() {
    super.layoutSubviews()
    layer.cornerRadius = bounds.width / 2.0
}

或者,如果使用的是视图控制器,请重写viewDidLayoutSubviews方法:

代码语言:javascript
复制
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    profileImageView.layer.cornerRadius = profileImageView.bounds.width / 2.0
}
票数 6
EN

Stack Overflow用户

发布于 2017-05-10 19:15:34

profileImageView还没有布局,也没有尺寸。假设这是在UIViewController子类中,您可以将角半径代码放在viewDidLayoutSubviews中。

代码语言:javascript
复制
override func viewDidLayoutSubviews() {
    profileImageView.layer.cornerRadius = (profileImageView.frame.width / 2)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43900795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档