2014-10-30 82 views
1

我已動態創建一些控件出現在設備的底部,對於我需要給框架基於設備height.But當設備高度得到改變,這是複雜的所有控件,我的代碼是如何爲動態創建的控件實現自動佈局?

-(void)viewWillAppear:(BOOL)animated 
{ 

    if([[UIScreen mainScreen]bounds].size.height==568) 
    { 
     xaxis=25; 
     yaxis=350; 
    } 
    else 
    { 
     xaxis=25; 
     yaxis=260; 
    } 

    UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(xaxis+35, yaxis+35, 250, 50)]; 

    label1 [email protected]「This is first Label; 

    UILabel *label2=[[UILabel alloc]initWithFrame:CGRectMake(xaxis+100, yaxis+50, 120,50)]; 

    [email protected]"This is second Label"; 


    [self.view addSubview: label1]; 
    [self.view addSubview: label2]; 

} 

任何人都可以建議如何爲這些控件實現自動佈局,以及它應該在啓用個人熱點時進行調整。

+0

使用約束來管理控制3.5「和4」 – Spynet 2014-10-30 11:44:38

+0

使用[parentView addConstraint:[NSLayoutConstraint constraintWithItem:... – Astoria 2014-10-30 11:54:30

+0

可以你簡要解釋一下嗎? – StackUser 2014-10-30 12:01:59

回答

0

您需要以編程方式創建所有自動佈局約束。 只需使用addConstraint方法,你可以看到下面

CGFloat bottom = 10; 
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1 constant:bottom]]; 

您還需要創建添加其他限制也是如此。

+0

感謝您的回覆!我添加了底部值爲0的約束,我的標籤出現在底部和左側,只有一些文本出現。爲什麼它看起來像這樣?.Wheather我需要給NSLayoutAttributeLeft,NSLayoutAttributeWidth,NSLayoutAttributeHeight約束爲控件? – StackUser 2014-10-31 04:51:57

+0

@StackUser添加子視圖後嘗試使用[self layoutSubtreeIfNeeded]方法。 – Astoria 2014-10-31 07:54:13

+0

你可以編輯我的代碼,讓我的標籤出現在屏幕的底部(即標籤1上方的標籤2) – StackUser 2014-10-31 12:00:32