Ios 加载视图后在视图上设置角半径

Ios 加载视图后在视图上设置角半径,ios,swift,Ios,Swift,我有一个来自类的视图,我想将角半径设置为其宽度的一半 宽度是使用自动布局生成的计算属性。因此,通常我会在viewWillLayoutSubviews()中设置拐角半径属性,如下所示 override func viewWillLayoutSubviews() { super.viewWillLayoutSubviews() c.largeProfileImage.layer.cornerRadius = c.largeProfileImage.frame.width / 2 }

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

宽度是使用自动布局生成的计算属性。因此,通常我会在
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
}()

要获得正确的动画效果,请像这样更改序列

    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)

要获得正确的动画效果,请像这样更改序列

    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)

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

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

您可能会看到一个圆形的
UIImage
,但您也可能会注意到圆圈内的正方形图像,为了解决这个问题,您可能需要使用
contentMode
。大多数情况下,
.scaleSpectFill
完成了这项工作。

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

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

您可能会看到一个圆形的
UIImage
,但您也可能会注意到圆圈内的正方形图像,为了解决这个问题,您可能需要使用
contentMode
。大多数情况下,
.scaleSpectFill
完成了这项工作。

我成功地调试了这个问题

因为宽度是一个计算属性,所以在布局之前它是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

我成功地调试了这个问题

因为宽度是一个计算属性,所以在布局之前它是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


从类视图添加视图在声明类的同一控制器上?或者在控制器A中添加类,并将类的视图从控制器A添加到控制器B?是否尝试在viewDidLayoutSubviews方法中添加cornerRadius?请尝试此方法,可能在所有视图布局后设置拐角半径会有所帮助您需要添加
largeProfileImage.layer.masksToBounds=false
largeProfileImage.clipsToBounds=true
从类视图添加视图在声明类的同一控制器上?或者在控制器A中添加类,并将类的视图从控制器A添加到控制器B?是否尝试在viewDidLayoutSubviews方法中添加cornerRadius?请尝试此方法,在所有视图布局完毕后,设置拐角半径可能会有所帮助。您需要添加
largeProfileImage.layer.masksToBounds=false
largeProfileImage.clipsToBounds=true
动画效果很好,可以进行动画制作。问题是,在设置角半径后,角半径没有得到setadd c.largeProfileImage.clipsToBounds=true。另外,为什么要在完成处理程序中调用layoutIf required。这不是必需的。动画很好,可以在其中设置动画。问题是,在设置角半径后,角半径没有得到setadd c.largeProfileImage.clipsToBounds=true。另外,为什么要在完成处理程序中调用layoutIf required。这不是必需的。创建视图时无法访问self。。。将在编辑中包含该视图。在您的更新中,它应该是
masksToBounds=false
我相信这与我从类中获取视图以及我没有正确引用视图这一事实有关。或者,宽度是一个计算属性,并且在宽度等于0时设置角半径。但是,我无法在函数中调用viewWillLayoutSubviews。您能否对TranslatesAutoResizezingMaskintoConstraints=false进行注释并进行测试?问题仍然存在。创建视图时无法访问self。。。将在编辑中包含该视图。在您的更新中,它应该是
masksToBounds=false
我相信这与我从类中获取视图以及我没有正确引用视图这一事实有关。或者,宽度是一个计算属性,并且在宽度等于0时设置角半径。但是,我无法在函数中调用viewWillLayoutSubviews。您能否对TranslatesAutoResizezingMaskintoConstraints=false进行注释并测试?问题仍然存在。