StoreSearch: confused by use of CGRect.zero

On page 45, we override awakeFromNib() in the SearchResultCell class in order to customize the cell selection color:

override func awakeFromNib() {
    super.awakeFromNib()

    let selectedView = UIView(frame: CGRect.zero)
    selectedView.backgroundColor = UIColor(red: 20/255, green: 160/255, blue: 160/255, alpha: 0.5)
    selectedBackgroundView = selectedView
}

What I find confusing is the use of CGRect.zero, which seems to indicate that we’re creating a UIView with a size of zero, located at the (0, 0) coordinate within its superview. How is it that this new view covers the entire cell, then? Is this something that the selectedBackgroundView property handles on its own?

Correct. You’re making a view with a size of 0x0. The UITableViewCell itself will take care of resizing it so that it is the same size as the cell. It’s a bit of magic that goes on behind the scenes.

1 Like

I sort of figured as much, but it had always puzzled me nevertheless. Thanks for the confirmation.