View has wrong frame when loaded from xib and shown first time - ios

I created a view to show in MapView annotation callout.
If I didn't use xib and instantiate this view with initWithFrame, then there is no problem.
But if I use xib, size shown on map is bigger, than is should be.
Code in view:
#interface CalloutContentView : UIView
#property (nonatomic) Visit *visit;
+ (id)customView;
#end
#import "CalloutContentView.h"
#interface CalloutContentView ()
#property (weak, nonatomic) IBOutlet UILabel *title;
#property (weak, nonatomic) IBOutlet UILabel *subtitle;
#property (weak, nonatomic) IBOutlet UISegmentedControl *sc;
#end
#implementation CalloutContentView
+ (id)customView
{
CalloutContentView *customView = [[[NSBundle mainBundle] loadNibNamed:#"CalloutContentView" owner:self options:nil] lastObject];
if ([customView isKindOfClass:[CalloutContentView class]])
return customView;
else
return nil;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// _title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, w, 18)];
_title.font = [UIFont boldSystemFontOfSize:15.0];
// _title.textAlignment = NSTextAlignmentLeft;
_title.textColor = [UIColor colorWithWhite:0.199 alpha:1.000];
_title.numberOfLines = 1;
_title.backgroundColor = [UIColor yellowColor];
[self addSubview:_title];
// _subtitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 25.0, w, 14)];
_subtitle.font = [UIFont systemFontOfSize:12.0];
_subtitle.textAlignment = NSTextAlignmentLeft;
_subtitle.textColor = [UIColor colorWithWhite:0.239 alpha:1.000];
_subtitle.backgroundColor = [UIColor clearColor];
[self addSubview:_subtitle];
// _sc = [[UISegmentedControl alloc]initWithItems:#[#"One",#"Two"]];
_sc.frame = CGRectMake(0, self.frame.size.height - 30, w, 30);
[_sc addTarget:self action:#selector(segmentedControlValueDidChange:) forControlEvents:UIControlEventValueChanged];
[self addSubview:_sc];
}
return self;
}
# end
And in MapViewController:
- (void)popupMapCalloutView {
// Change this for creating your Callout View
CalloutContentView *customView = [CalloutContentView customView];
// CalloutContentView *customView = [[CalloutContentView alloc]initWithFrame: CGRectMake(0, 0, 10, 90)];
// if you provide a custom view for the callout content, the title and subtitle will not be displayed
_calloutView.contentView = customView;
VisitAnnotationView *bottomPin = _visitAnnotationViews[_idAnn];
bottomPin.calloutView = _calloutView;
[_calloutView presentCalloutFromRect:bottomPin.bounds
inView:bottomPin
constrainedToView:_mapView
permittedArrowDirections:SMCalloutArrowDirectionAny
animated:YES];
}
That's how it look like when xib is used:

Related

dynamic cell height for custom uitableviewcell not working

I created a custom tableview cell and the code is given below. I am trying to add a new UIView named bottomTextContainer to the contentview of the cell.The bottomTextContainer holds the labels of published date and source of the article. my problem is bottomTextContainer view is not showing inside the cell it is overlapping with the next cell.
ArticleCell.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface ArticleCell : UITableViewCell
#pragma mark- Properties
#property (strong, nonatomic) UILabel *source;
#property (strong, nonatomic) UILabel *publishedDate;
#pragma mark- Methods
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier;
#end
NS_ASSUME_NONNULL_END
ArticleCell.m
#import "ArticleCell.h"
#interface ArticleCell ()
#pragma mark- Private Properties
#property (strong, nonatomic) UIView *bottomTextContainer;
#end
#implementation ArticleCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
[self createSubViews];
}
return self;
}
- (void)createSubViews{
self.textLabel.numberOfLines = 0;
self.textLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:20];
self.textLabel.textColor = [UIColor blackColor];
self.textLabel.textAlignment = NSTextAlignmentLeft;
self.detailTextLabel.numberOfLines = 0;
self.detailTextLabel.font = [UIFont fontWithName:#"HelveticaNeue" size:16];
self.detailTextLabel.textColor = [UIColor blackColor];
self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
self.bottomTextContainer = [[UIView alloc] init];
[self.contentView addSubview:self.bottomTextContainer];
self.bottomTextContainer.translatesAutoresizingMaskIntoConstraints = false;
self.source = [[UILabel alloc] init];
[self.bottomTextContainer addSubview:self.source];
self.source.textColor = [UIColor blackColor];
self.source.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
self.source.textAlignment = NSTextAlignmentLeft;
self.source.translatesAutoresizingMaskIntoConstraints = false;
self.publishedDate = [[UILabel alloc] init];
[self.bottomTextContainer addSubview:self.publishedDate];
self.publishedDate.textColor = [UIColor blackColor];
self.publishedDate.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12];
self.publishedDate.textAlignment = NSTextAlignmentRight;
self.publishedDate.translatesAutoresizingMaskIntoConstraints = false;
}
- (void)layoutSubviews{
[super layoutSubviews];
UILayoutGuide *contentViewLayout = self.contentView.layoutMarginsGuide;
[self.bottomTextContainer.topAnchor constraintEqualToAnchor:self.detailTextLabel.bottomAnchor].active = YES;
[self.bottomTextContainer.leadingAnchor constraintEqualToAnchor:contentViewLayout.leadingAnchor].active = YES;
[self.bottomTextContainer.trailingAnchor constraintEqualToAnchor:contentViewLayout.trailingAnchor].active = YES;
[self.source.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
[self.source.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
[self.source.leadingAnchor constraintEqualToAnchor:self.bottomTextContainer.leadingAnchor].active = YES;
[self.source.widthAnchor constraintEqualToAnchor:self.bottomTextContainer.widthAnchor multiplier:0.5].active = YES;
[self.publishedDate.topAnchor constraintEqualToAnchor:self.bottomTextContainer.topAnchor constant:10].active = YES;
[self.publishedDate.bottomAnchor constraintEqualToAnchor:self.bottomTextContainer.bottomAnchor constant:10].active = YES;
[self.publishedDate.trailingAnchor constraintEqualToAnchor:self.bottomTextContainer.trailingAnchor].active = YES;
[self.publishedDate.leadingAnchor constraintEqualToAnchor:self.source.trailingAnchor].active = YES;
}
#end
This is the result I am getting. Show
Try this:
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 44;

When a custom button is pressed UILabel won't update (Objective-C)

(I'm currently learning iOS Development so I apologize in advanced of my limited knowledge.)
What I am trying to accomplish is that whenever you press either the addition or subtraction button the label will increment by one and will display the result by the label. I am having difficulty with displaying the result in the label.
This is my ViewController.h file:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController{
UILabel *valueLabel;
}
#end
This is my ViewController.m file:
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
int w;
w = 0;
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
// AddButton
UIButton *additionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
additionButton.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:additionButton];
// Subtraction button
UIButton *subtractionButton = [[UIButton alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
subtractionButton.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 25, 100, 50);
[self.view addSubview:subtractionButton];
// Label
valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:valueLabel];
}
#end
This is my Delegate and Protocol:
StepperView.h
#import <UIKit/UIKit.h>
#protocol SwitchViewDelegate
- (void)switchViewValueChanged:(int)number;
#end
#interface StepperView : UIView
{
}
#property (nonatomic,weak) id<SwitchViewDelegate> delegate;
#end
This is my StepperView.m file
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:leftView];
// Label
UILabel *valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(stepperView.frame.size.width/2, stepperView.frame.size.height/2, 100, 100)];
[valueLabel setText:#"10"];
[valueLabel setTextColor:[UIColor redColor]];
[self addSubview:valueLabel];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
NSLog(#"MINUS");
} else {
NSLog(#"PLUS");
}
// NSLog(#"%#",title);
}
#end
I am not sure if i understand clear what you want to do, but why don't you try to use the UIStepper button provided by iOS?
You can add an IBAction to your viewController and change the label with the value from the UIStepper.
Using swift:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var valueLabel: UILabel!
var currentValue = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func touchStepper(sender: UIStepper) {
self.valueLabel.text = "\(sender.value)"
}
}
It's not very clear what you're trying you obtain as UI, i'm posting a possible solution which is using
The delegate to pass the information from StepperView to the ViewController
UILabel to present the result which is subview of ViewController's view, instead to be subview of StepperView
ViewController.h:
#import <UIKit/UIKit.h>
#import "StepperView.h"
#interface ViewController : UIViewController
#property (nonatomic, strong) UILabel *valueLabel;
#end
ViewController.m
#import "ViewController.h"
#import "StepperView.h"
#interface ViewController () <StepperViewDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
StepperView *stepperView = [[StepperView alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
stepperView.center = self.view.center;
[self.view addSubview:stepperView];
stepperView.delegate = self;
// Label
self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(195, 318, 100, 100)];
[self.valueLabel setTextColor:[UIColor redColor]];
[self.view addSubview:self.
valueLabel];
}
#pragma mark - StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value {
self.valueLabel.text = [NSString stringWithFormat:#"%d", value];
}
#end
StepperView.h
#import <UIKit/UIKit.h>
#class StepperView
#protocol StepperViewDelegate
- (void)stepperView:(StepperView *)stepperView valueChanged:(NSInteger)value
#end
#interface StepperView : UIView
#property (nonatomic, weak) id<StepperViewDelegate> delegate;
#property (nonatomic, assign) NSInteger stepperValue;
#end
StepperView.m
#import "StepperView.h"
#implementation StepperView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
[self setup];
return self;
}
- (void)setup {
UIView *stepperView = [[UIView alloc] init];
stepperView.frame = CGRectMake(0, 0, 300, 40);
stepperView.backgroundColor = [UIColor colorWithRed:21/255.0 green:101/255.0 blue:192/255.0 alpha:1.0];
[self addSubview:stepperView];
// Add Button
UIButton *rightView = [[UIButton alloc] init];
rightView.frame = CGRectMake(stepperView.frame.size.width/2 + 50, stepperView.frame.size.height/2 - 20, 100, 40);
rightView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[rightView setTitle:#"+" forState:UIControlStateNormal];
[rightView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:rightView];
// Subtract Button
UIButton *leftView = [[UIButton alloc] init];
leftView.frame = CGRectMake(stepperView.frame.size.width/2 - 150, stepperView.frame.size.height/2 - 20, 100, 40);
leftView.backgroundColor = [UIColor colorWithRed:33/255.0 green:150/255.0 blue:243/255.0 alpha:1.0];
[leftView setTitle:#"-" forState:UIControlStateNormal];
[leftView addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)buttonPressed:(id)sender{
UIButton *button = (UIButton *) sender;
NSString *title = button.titleLabel.text;
if([title isEqualToString:#"-"]) {
self.stepperValue--;
} else {
self.stepperValue++;
}
[self.delegate stepperView:self valueChanged:self.stepperValue];
// NSLog(#"%#",title);
}
#end
This should give you more clear vision how different components should corespondent with each other.

TTTAttributedLabel Delegate didSelectLinkWithURL not getting called

I'm having problem setting up the TTTAttributedLabel within my custom cell. I have already checked many post and still didn't figured how to solve this. (I can see the URL styling at this custom label , but if i click on it nothing happen).
CustomCell.h:
#interface MyCustomCell : UITableViewCell<TTTAttributedLabelDelegate>
#property (nonatomic, strong, readonly) UITapGestureRecognizer *tapRecognizer;
#property (nonatomic, strong, readonly) UILabel *senderLabel;
#property (nonatomic, strong, readonly) UILabel *timeLabel;
#property (nonatomic, strong, readonly) UILabel *dateLabel;
#property (nonatomic, strong) TTTAttributedLabel *customTextLabel;
#end
CustomCell.m:
#implementation MyCustomCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
_dateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_dateLabel.textAlignment = NSTextAlignmentCenter;
_dateLabel.font = [UIFont boldSystemFontOfSize:12.0];
_dateLabel.textColor = [UIColor grayColor];
_dateLabel.text = #"2015-10-10";
_dateLabel.userInteractionEnabled = false;
_senderLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_senderLabel.textAlignment = NSTextAlignmentLeft;
_senderLabel.font = [UIFont boldSystemFontOfSize:14.0];
_senderLabel.textColor = [UIColor whiteColor];
_senderLabel.userInteractionEnabled = false;
_timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_timeLabel.userInteractionEnabled = false;
_timeLabel.textAlignment = NSTextAlignmentCenter;
_timeLabel.font = [UIFont boldSystemFontOfSize:11.0];
_timeLabel.textColor = [UIColor whiteColor];
_customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectZero];
_customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
_customTextLabel.delegate = self;
_customTextLabel.backgroundColor = [UIColor clearColor];
_customTextLabel.numberOfLines = 0;
_customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
_customTextLabel.textColor = [UIColor blackColor];
_customTextLabel.font = [UIFont systemFontOfSize:18.0];
[self.textLabel addSubview:_customTextLabel];
_customTextLabel.userInteractionEnabled = true;
self.imageView.userInteractionEnabled = YES;
self.imageView.layer.cornerRadius = 5.0;
self.imageView.layer.masksToBounds = YES;
}
return self;
}
Thank you for help.
NSString *text = #"Hello this is https://www.google.com";
_customTextLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 100, 300, 20)];
_customTextLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink;
_customTextLabel.delegate = self;
_customTextLabel.backgroundColor = [UIColor clearColor];
_customTextLabel.numberOfLines = 0;
_customTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
_customTextLabel.textColor = [UIColor blackColor];
_customTextLabel.font = [UIFont systemFontOfSize:18.0];
_customTextLabel.text = text;
[self.view addSubview:_customTextLabel];
And set delegate method of TTTAttributedLabel it works fine please check:
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
}

Difficulty setting property in custom UITableViewCell

I have a custom UITableViewCell that contains several labels and an image view that can be one of three icons depending on a value in each item represented by the cell. All of my labels are updating properly, but the value of the image name is always nil. The image name is declared as a property of type NSString.
The custom cell is called FavoriteCell and all the values are setting properly except the NSString value "imgName"
FavoriteCell.h:
#interface FavoriteCell : UITableViewCell
#property (nonatomic, strong) UILabel *lblMainTitle;
#property (nonatomic, strong) UILabel *lblGaugeID;
#property (nonatomic, strong) UILabel *lblGaugeLastUpdate;
#property (nonatomic, strong) UILabel *lblGaugeHeight;
#property (nonatomic, strong) UILabel *lblGaugeCFS;
#property (nonatomic, strong) UIImage *bgImage;
#property (nonatomic, strong) UIImageView *goodToGoImage;
#property (nonatomic, strong) NSString *imgName;
#end
FavoriteCell.m:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
bgImage = [UIImage imageNamed:#"tableCellBG.png"];
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UIColor *transparentBG = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0];
CGSize size = self.contentView.frame.size;
//self.backgroundView = [[UIImageView alloc] initWithImage:bgImage];
self.lblMainTitle = [[UILabel alloc] initWithFrame:CGRectMake(8.0, -10, size.width-20, size.height-40)];
[self.lblMainTitle setFont:[UIFont boldSystemFontOfSize:15]];
[self.lblMainTitle setTextAlignment:NSTextAlignmentLeft];
[self.lblMainTitle setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
[self.lblMainTitle setBackgroundColor:transparentBG];
self.lblGaugeID = [[UILabel alloc] init];
self.lblGaugeLastUpdate = [[UILabel alloc] initWithFrame:CGRectMake(9, 14, size.width-40, size.height)];
[self.lblGaugeLastUpdate setFont:[UIFont systemFontOfSize:14]];
[self.lblGaugeLastUpdate setBackgroundColor:transparentBG];
self.lblGaugeHeight = [[UILabel alloc] initWithFrame:CGRectMake(8, 45, size.width-40, size.height)];
[self.lblGaugeHeight setFont:[UIFont boldSystemFontOfSize:21]];
[self.lblGaugeHeight setBackgroundColor:transparentBG];
self.lblGaugeCFS = [[UILabel alloc] initWithFrame:CGRectMake(120, 45, size.width-40, size.height)];
[self.lblGaugeCFS setFont:[UIFont boldSystemFontOfSize:21]];
[self.lblGaugeCFS setBackgroundColor:transparentBG];
NSLog(#"IMAGE NAME: %#\n", imgName); // imgName is nil
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 40, 40, 40)];
[imgView setImage:[UIImage imageNamed:imgName]];
[self.contentView addSubview:lblMainTitle];
[self.contentView addSubview:lblGaugeHeight];
[self.contentView addSubview:lblGaugeLastUpdate];
[self.contentView addSubview:lblGaugeCFS];
[self.contentView addSubview:imgView];
self.editingAccessoryType = UITableViewCellAccessoryDetailButton;
}
return self;
}
The values are being set in my table view controller's cellForRowAtIndexPath method shown here. For now, I'm just hard coding a value for testing. The hard coded value is not carrying over to the custom cell.
-(FavoriteCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FavoriteCell *cell = (FavoriteCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
if(cell == nil){
cell = [[FavoriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
Favorite *fave = [allFavoriteObjects objectAtIndex:indexPath.row];
NSString *currFeet = [NSString stringWithFormat:#"%.02fft", [fave.currentFeet doubleValue]];
NSString *currFlow = [NSString stringWithFormat:#"%dcfs", [fave.currentCFS intValue]];
[cell setImgName:#"thumbsUp.png"];
[cell.lblMainTitle setText:[fave stationRealName]];
[cell.lblGaugeID setText:[fave stationIdentifier]];
[cell.lblGaugeCFS setText:currFlow];
[cell.lblGaugeHeight setText:currFeet];
return cell;
}
Have I missed something obvious? Thanks!
that is normal because you code is running when you don't have set the imgName, you set it then that your init code is run.
fox fix it remove the code UIImage and the NSString from you custom cell and set it into your imageView then when you make the cell into the -tableView:cellForRowAtIndexPath: by imageView.image = [UIImage imageNamed:#"thumbsUp.png"];
Well, upon first glance, the moment at which you are saying imgName logs as nil seems to be because you are not actually setting the imgName until after initWithStyle is called. Or is there something I'm missing about what you're expecting to happen?

iOS Bounds of UIView in ViewDidLoad

I wanted to use the bounds of a UIView to create a UILabel and add it to that UIView inside the viewDidLoad method, however, I have found that the bounds of UIView is still null inside viewDidLoad. But when I look at a demo app, the bounds of its UIView is already initialised in viewDidLoad.
In the interface I have
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *promotionView;
#end
And I am trying to do
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *container = [[UIView alloc] initWithFrame:self.promotionView.bounds];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, container.bounds.size.height-100, 320, 20)];
label.text = #"Promotion Title";
[container addSubview:label];
[self.promotionView addSubview: container];
}
but it doesn't work because the bounds of promotionView is null.
You can do it like this in viewDidAppear:
-(void)viewDidAppear:(BOOL)animated {
static int first = 1;
if (first) {
UIView *container = [[UIView alloc] initWithFrame:self.promotionView.bounds];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, container.bounds.size.height-100, 320, 20)];
label.text = #"Promotion Title";
[container addSubview:label];
[self.promotionView addSubview: container];
first = 0;
}
}

Resources