2013-12-19 52 views
1

我做了下面的代碼,在屏幕底部獲取標籤,具體取決於設備是iphone5還是iphone4/4s。在屏幕底部放置一個標籤

我想知道我有另一種方式....例如自動佈局?

- (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]; 

} 
+0

是存在與自動佈局另一種方式 – rocky

回答

0

是的。你絕對不想在位置值中進行硬編碼。使用界面構建器將約束添加到標籤。蘋果關於自動佈局的文檔非常好,並且可以給你提供一些想法。

0

代替硬編碼值或使用約束和Interface Builder中,你可以使用一個簡單的數學表達式,像這樣:

CGFloat const labelHeight = 100; 
CGRect frame = CGRectMake(0, CGRectGetHeight([[UIScreen mainScreen] bounds]) - labelHeight, 320, labelHeight); 
3
- (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]; 

} 
相關問題