I am familiar with using storyboards to construct UI elements in Xcode, but I am only beginning to learn how to make UI programmatically (without a storyboard).
I wanted to know if this is the correct way to implement a UIView into an existing ViewController class.
DetailView (Custom UIView class):
.h file (header)
@interface DetailView : UIView @property (strong, nonatomic) UIImageView *imageView; @property (strong, nonatomic) UIImageView *ratingView; @property (strong, nonatomic) UILabel *tagLabel; @property (strong, nonatomic) UILabel *plotLabel; @end
.m file (implementation)
- (instancetype)init { if(self = [super init]){ // Creating the large movie image _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 64.0, 375.0f, 200.0f)]; // Creating the rating image _ratingView = [[UIImageView alloc] init]; // Creating the tag Label _tagLabel = [[UILabel alloc] initWithFrame:CGRectMake(38, 274, 355, 200)]; self.tagLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:12]; // Creating the plot label _plotLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 299, 355, 200)]; self.plotLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14]; [self addSubview:self.imageView]; [self addSubview:self.ratingView]; [self addSubview:self.tagLabel]; [self addSubview:self.plotLabel]; } return self; }
View Controller:
Commented code is another way I attempted to implement the DetailView
- (void)viewDidLoad { [super viewDidLoad]; self.detailView = [[DetailView alloc] init]; self.title = self.movie.title; self.view.backgroundColor = [UIColor whiteColor]; self.detailView.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.movie.fanArt]]]; //[self.view addSubview:self.detailView.imageView]; [self setRatingImage:self.movie.rating]; //[self.view addSubview:self.detailView.ratingView]; self.detailView.tagLabel.numberOfLines = 0; self.detailView.tagLabel.text = self.movie.tagline; [self.detailView.tagLabel sizeToFit]; //[self.view addSubview:self.detailView.tagLabel]; self.detailView.plotLabel.numberOfLines = 0; self.detailView.plotLabel.text = self.movie.plot; [self.detailView.plotLabel sizeToFit]; //[self.view addSubview:self.detailView.plotLabel]; [self.view addSubview:self.detailView]; }