David Seek David Seek - 2 months ago 56
Swift Question

Swift / cornerRadius and masksToBounds lets UIImageView disappear

I have a

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

@IBOutlet weak var avatarImageView: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()

updateUI()
}

func updateUI() {

backendless.userService.currentUser.retrieveProperties()

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

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

tableView.reloadData()

}


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.:

PS:



Xcode 8.0

Swift 2.3

Contrains for ImageView are set to 120*120

sd_setImageWithURL
=
pod 'SDWebImage'


enter image description here

Answer

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.

Comments