Search code examples
iosswiftxcodeuiimageviewuiimage

why won't right, left, width, height work on UIImageView?


I'm following a tutorial for a messaging chat app and I keep getting errors on the UIImageViews. For example ~ userImageView.right, userImageView.width, etc. all get errors saying UIImageView has no member. I've used the right/width/height in other parts of my app, I can't figure out why it's not happy here... thoughts?

class ConversationTableViewCell: UITableViewCell {

static let identifier = "ConversationTableViewCell"

private let userImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.contentMode = .scaleAspectFill
    imageView.layer.cornerRadius = 50
    imageView.layer.masksToBounds = true
    return imageView
}()

private let userNameLabel: UILabel = {
    let label = UILabel()
    label.font = .systemFont(ofSize: 21, weight: .semibold)
    return label
}()

private let userMessageLabel: UILabel = {
    let label = UILabel()
    label.font = .systemFont(ofSize: 19, weight: .regular)
    label.numberOfLines = 0
    return label
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    contentView.addSubview(userImageView)
    contentView.addSubview(userNameLabel)
    contentView.addSubview(userMessageLabel)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
    super.layoutSubviews()

    userImageView.frame = CGRect(x: 10,
                                 y: 10,
                                 width: 100,
                                 height: 100)

    userNameLabel.frame = CGRect(x: userImageView.right + 10,
                                 y: 10,
                                 width: contentView.width - 20 - userImageView.width,
                                 height: (contentView.height-20)/2)

    userMessageLabel.frame = CGRect(x: userImageView.right + 10,
                                    y: userNameLabel.bottom + 10,
                                    width: contentView.width - 20 - userImageView.width,
                                    height: (contentView.height-20)/2)

}

Solution

  • Create a new swift file and add following code in that and everything will be good to go.

    extension UIView {
        public var width: CGFloat {
            return frame.size.width
        }
        
        public var height: CGFloat {
            return frame.size.height
        }
        
        public var top: CGFloat {
            return frame.origin.y
        }
    
        public var left: CGFloat {
            return frame.origin.x
        }
    
        public var bottom: CGFloat {
            return top + height
        }
    
        public var right: CGFloat {
            return left + width
        }
    }
    

    Or:

    More easy way to just import this package AATools in your project, it contains many handy extensions for speedy development.