2014-10-30 66 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