【问题标题】:Set corner radius on view after view loads视图加载后在视图上设置角半径
【发布时间】:2017-08-10 04:09:43
【问题描述】:

我有一个来自班级的视图,我想将角半径设置为宽度的一半。

宽度是使用自动布局生成的计算属性。所以通常我会像这样在viewWillLayoutSubviews() 中设置圆角半径属性

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    c.largeProfileImage.layer.cornerRadius = c.largeProfileImage.frame.width / 2
}

但 largeProfileImage 不是在 viewdidLoad 之后调用的初始视图,我通过点击手势对其进行动画处理。下面是视图在屏幕上动画的位置。它是在同一个函数中创建的。

     //I tried setting the cornerRadius here as well but it isn't setting.

    //c.largeProfileImage.layer.cornerRadius = c.largeProfileImage.frame.width / 2


    self.view.layoutIfNeeded()

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: {

        self.profileImageContainerCenterY?.constant = -(c.profileImageContainer.frame.height) * 2
        self.profileSettingsContainerCenterY?.constant = 0

        c.profileSettingsContainer.alpha = 1
        c.largeProfileImage.alpha = 1

        self.view.layoutIfNeeded()
    }, completion: { (completed) in
        self.view.layoutIfNeeded()
})

编辑:

这是个人资料图片

let largeProfileImage: UIImageView = {
    let pv = UIImageView()
    pv.contentMode = .scaleAspectFill
    pv.layer.masksToBounds = true
    pv.clipsToBounds = true
    pv.image = UIImage(named: "user")

    pv.translatesAutoresizingMaskIntoConstraints = false
    return pv
}()

【问题讨论】:

  • 从类视图添加视图是在声明类的同一控制器上?或者在控制器 A 中添加类以及从控制器 A 到控制器 B 的类视图?
  • 你试过在 viewDidLayoutSubviews 方法中添加cornerRadius吗?请在这个方法中尝试,可能会有助于在所有视图布局后设置cornerRadius
  • 你需要添加largeProfileImage.layer.masksToBounds = falselargeProfileImage.clipsToBounds = true

标签: ios swift


【解决方案1】:

我能够成功调试问题。

由于宽度是一个计算属性,所以在布局布局之前它是 0,这意味着

c.largeProfileImage.layer.cornerRadius = c.largeProfileImage.frame.width / 2

self.view.layoutIfNeeded()

导致圆角半径为零。

所以解决方案...

是在之后调用宽度是在计算宽度之后设置拐角半径。

self.view.layoutIfNeeded()

c.largeProfileImage.layer.cornerRadius = c.largeProfileImage.frame.width / 2

【讨论】:

    【解决方案2】:

    为了正确的动画改变这样的顺序。

        self.profileImageContainerCenterY?.constant = -(c.profileImageContainer.frame.height) * 2
        self.profileSettingsContainerCenterY?.constant = 0
    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
        c.profileSettingsContainer.alpha = 1
        c.largeProfileImage.alpha = 1
        self.view.layoutIfNeeded()
    }, completion: nil)
    

    【讨论】:

    • 动画效果很好。问题是角半径没有设置
    • 设置圆角半径后添加 c.largeProfileImage.clipsToBounds = true。
    • 还有你为什么要在完成处理程序中调用 layoutIf 。这不是必需的。
    【解决方案3】:

    下面三行是获得圆形图像所必需的-

    image.layer.cornerRadius = image.frame.width / 2
    image.layer.masksToBounds = false
    image.clipsToBounds = true
    

    您可能会看到圆形的UIImage,但您也可能会注意到圆圈内的方形图像,并解决您可能玩过contentMode 的问题。大多数时候.scaleAspectFill 完成这项工作。

    【讨论】:

    • 创建视图时无法访问自己...将在编辑中包含该视图。
    • 在你的更新中应该是masksToBounds = false
    • 我相信这与我从一个类中获取视图并且我没有正确引用视图这一事实有关。或者宽度是一个计算属性并且当宽度等于0时设置角半径的事实。我不能在函数中调用viewWillLayoutSubviews。
    • 你能评论translatesAutoresizingMaskIntoConstraints = false并测试吗?
    • 问题仍然存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多