Subclass UIView or UIViewController

2019-03-16 23:59发布

问题:

I want to make a "section" which has an image and some text below it.

Should I be subclassing UIView or UIViewController? I'm thinking UIView as it won't know what image or what text to display, just that there will be both. Also if it is UIView, what method do I add the subviews (UIImageView and UILabel) in as subviews? Or should I draw the the text and image, without using UIImageView or UILabel in drawRect?

回答1:

  • Subclass UIView
  • Add the subviews (UIImageView and UILabel) in initWithFrame:
  • Layout the subviews in layoutSubviews
  • Do additional custom drawing in drawRect:, if needed


回答2:

Use UIView, because it's simpler. I would use UIViewController if I had multiple sections, such as tabs or something.