Swift3 iOS - 导航 TitleView 中的圆形 ImageView 一直显示方形?

Swift3 iOS -Rounded ImageView in Navigation TitleView Keeps Showing Square?

我有一个圆形的 imageView,用于我的应用程序中的个人资料图片。我在整个应用程序的 tableView 单元格中和视图控制器中的不同视图中多次使用代码。个人资料图片始终显示为圆形。当我使用下面的代码将个人资料图片设置为在 navItem 的 titleView 内圆形时,它只显示为正方形。

为什么在 navItem 的 titleView 中不显示圆形?

var url: String?//it's already set with some value

override func viewDidLoad() {
        super.viewDidLoad(){

        setTitleView(urlStr: url)
{

func setTitleView(urlStr: String?){

        let titleView = UIView()
        titleView.frame = CGRect(x: 0, y: 0, width: 44, height: 44)

        let containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        titleView.addSubview(containerView)

        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill
        imageView.layer.cornerRadius = imageView.frame.size.width / 2
        imageView.clipsToBounds = true
        imageView.layer.borderColor = UIColor.white.cgColor
        imageView.layer.borderWidth = 0.5
        imageView.backgroundColor = UIColor.white

        if let urlStr = urlStr{
            let url = URL(string: urlStr)
            imageView.sd_setImage(with: url!)
        }

        containerView.addSubview(imageView)

        imageView.leftAnchor.constraint(equalTo: containerView.leftAnchor).isActive = true
        imageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor).isActive = true
        imageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
        imageView.heightAnchor.constraint(equalToConstant: 40).isActive = true

        containerView.centerXAnchor.constraint(equalTo: titleView.centerXAnchor).isActive = true
        containerView.centerYAnchor.constraint(equalTo: titleView.centerYAnchor).isActive = true

        navigationItem.titleView = titleView
}

看起来 imageView.layer.cornerRadius = imageView.frame.size.width / 2 设置为 0imageView.frame.size.width 等于 0 这里。

而不是 let imageView = UIImageView(),您可以在使用 let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))

创建 imageView 时预定义框架