我有一個來自類的視圖,我想將角半徑設置爲其寬度的一半。在視圖加載後在視圖中設置角點半徑
寬度是使用自動佈局製作的計算屬性。所以,一般情況下我設定的圓角半徑財產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()
})
編輯:
這裏的profileImage
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 – 3stud1ant3
您需要添加'largeProfileImage.layer.masksToBounds = false'和'largeProfileImage .clipsToBounds = true' – Annjawn