marc0
marc0

Reputation: 21

Place a label on the bottom of the screen

I made the following code to get a label on the bottom of the screen, depending if the device is a iphone5 or iphone4/4s.

I like to know I there is another way.... for instance with auto-layout?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    CGRect frame;
    if ([UIScreen mainScreen].bounds.size.height == 568) {
        frame = CGRectMake(140, 500, 320, 100);
    } else {
        frame = CGRectMake(140, 410, 320, 100);
    }

    UILabel *iconAdLabel = [[UILabel alloc]init];
    iconAdLabel.frame = frame;
    iconAdLabel.font = [[UIFont preferredFontForTextStyle:UIFontTextStyleBody] fontWithSize:11.0];
    iconAdLabel.text = @"SOME NICE TEXT HERE";

    [self.view addSubview:iconAdLabel];

}

Upvotes: 1

Views: 1124

Answers (3)

adali
adali

Reputation: 5977

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    UILabel *iconAdLabel = [[UILabel alloc]init];
    iconAdLabel.frame = CGRectMake(0,self.view.frame.size.height-100,320,100);
    iconAdLabel.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;

    [self.view addSubview:iconAdLabel];

}

Upvotes: 3

Aaron Wojnowski
Aaron Wojnowski

Reputation: 6480

Instead of hardcoding values or using constraints and interface builder, you could use a simple math expression like so:

CGFloat const labelHeight = 100;
CGRect frame = CGRectMake(0, CGRectGetHeight([[UIScreen mainScreen] bounds]) - labelHeight, 320, labelHeight);

Upvotes: 0

Choppin Broccoli
Choppin Broccoli

Reputation: 3076

Yes indeed. You definitely DO NOT want to hard code in position values. Use interface builder to add constraints to the label. Apple's documentation on Auto-layout is quite good and can give you ideas as well.

Upvotes: 0

Related Questions