skip to Main Content

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)

}

2

Answers


  1. A UIImageView is a UIView object. As such it has a frame property, which is a CGRect.

    You can use someImageView.frame to get to the view’s frame.

    CGRects have properties size.height and size.width. They also have properties maxX and maxY, which are equivalent to right and bottom. (actually, it seems CGRect also has properties height and width, so you can skip the "size." prefix.)

    So for an imageView someImageView:

    height = someImageView.frame.height 
    width = someImageView.frame.width
    right = someImageView.frame.maxX
    bottom = someImageView.frame.maxY
    

    If you want you could add an extension to UIView that added the properties height, width, right, and bottom as in @afaq’s answer. (Voted)

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search