我正在尝试圈出个人资料照片的图像视图。
在我限制UiScreen宽度之前,它工作正常。

所以这是代码

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var proPicH: NSLayoutConstraint! //Profile Picture Height
@IBOutlet weak var proPicW: NSLayoutConstraint! // Profile Picture Width

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    circleImage()
}

func circleImage() {
    let screenSize = UIScreen.mainScreen().bounds
    let screenWidth = screenSize.width
    let width = screenWidth / 2
    print("ScreenWidth: \(screenWidth)")
    proPicW.constant = width
    proPicH.constant = width

    print("H:\(proPicH.constant)")
    print("W:\(proPicW.constant)") //Here the height and width comes to my expectations

    imageView.layer.cornerRadius = imageView.bounds.width / 2
    imageView.clipsToBounds = true

    print("Height: \(imageView.bounds.height)") // Here the height and width becomes more
}
}




请帮助我使图像变圆

最佳答案

在设置图像视图角半径的时间点,其边界尚未更新以匹配约束。更改此行

imageView.layer.cornerRadius = imageView.bounds.width / 2




imageView.layer.cornerRadius = width / 2


因此,用于设置约束的相同值也将用于拐角半径。

请注意,如果您在其他一些代码中更新了约束,那么您还可以更新拐角半径以进行匹配。

10-08 07:46
查看更多