为什么UIImageview不是完美的圆圈?

问题描述 投票:0回答:5
let radius = self.bounds.width / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true

我应用了上面的代码但它创造了一个奇怪的形状。

enter image description here

我不知道为什么以及如何解决它。

ios swift uiimageview
5个回答
0
投票

如果想要完美的圆形视图必须是正方形。

如果您的视图具有宽高比,则在为视图加载所有约束后应用角半径。

为此,您可以将半径视图应用于viewDidAppear方法。或viewDidLayoutSubviews方法。或使用延迟应用半径。

更新

func delay(_ seconds: Double, completion: @escaping () -> ()) {
    DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
        completion()
    }
}

3
投票

如果宽度和高度相等,则此代码正常工作,将2.5更改为2

let radius = self.bounds.width / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true

0
投票

要制作UIImageView圈,你需要确保你UIImageView的高度和宽度应该相同(方形)。

使用除以2而不是2.5:

let radius = self.bounds.width / 2

0
投票
imgView.layer.borderWidth = 1
imgView.layer.masksToBounds = false
imgView.layer.borderColor = UIcolor.black.cgColor
imgView.layer.cornerRadius = self.frame.height/2
imgView.clipsToBounds = true
self.view.layoutIfNeeded()

尝试这个代码或者可能是你的代码是完美的只需将self.view.layoutIfNeeded()放在你的代码中


-1
投票

确保图像高度和宽度相同

override func viewDidLayoutSubviews() {
    let radius = yourImageOutlet.bounds.width / 2
    yourImageOutlet.layer.cornerRadius = radius
    yourImageOutlet.layer.masksToBounds = true
}
© www.soinside.com 2019 - 2024. All rights reserved.