David Seek David Seek - 1 year ago 197
Swift Question

Swift / cornerRadius and masksToBounds lets UIImageView disappear

I have a

with a
. The content is loaded with the following simple code.

@IBOutlet weak var avatarImageView: UIImageView!

override func viewDidLoad() {


func updateUI() {


avatarImageView.layer.cornerRadius = avatarImageView.frame.size.width/2
avatarImageView.layer.masksToBounds = true

let imgURL = NSURL(string: (backendless.userService.currentUser.getProperty("Avatar") as? String)!)



The ImageView does not appear.

But if I delete the following code, the UIImageView will be set and displayed correctly.

avatarImageView.layer.cornerRadius = avatarImageView.frame.size.width/2
avatarImageView.layer.masksToBounds = true

What am I missing? Help is very appreciated.:


Xcode 8.0

Swift 2.3

Contrains for ImageView are set to 120*120

pod 'SDWebImage'

enter image description here

Answer Source

One obvious problem is that you are calling updateUI too soon. In viewDidLoad, there is no UI. The view has loaded but that is all; it is not in the interface and no layout has yet taken place. Your code depends upon avatarImageView.frame, but we do not yet know what that is. You have constraints on the image view, but they have not yet taken effect! Thus you should postpone this call until after the interface is present, e.g. viewDidAppear or at least viewDidLayoutSubviews.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download