iPhone Orientation改变视图框架

时间:2012-05-17 05:40:05

标签: iphone

我想以编程方式设置UILabel的位置取决于方向。但是当我回到Portrait时,它不起作用。

这是我的代码:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown)
    {
        lblUserName.frame = CGRectMake(20, 200, 280, 44); 
        lblPassword.frame = CGRectMake(20, 246, 280, 44); 
    }

    else
    {
        lblUserName.frame = CGRectMake(20, 220, 280, 44); 
        lblPassword.frame = CGRectMake(20, 266, 280, 44); 
    }
}

3 个答案:

答案 0 :(得分:2)

有两种可能性。您可以以编程方式设置标签。并在 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}

中检查方向条件

方法。否则你可以使用自动调整大小的掩码。

答案 1 :(得分:1)

尝试使用此IF条件:

 if ( UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
 { 

 } 
 else {}

答案 2 :(得分:1)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//write code here
return YES;
}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
//write code here
}

使用这两种方法在旋转设备时设置标签。

相关问题