1

我的 UIView 上有几个组件,例如文本字段、标签、表格、按钮。我想处理设备方向的变化。如何使这些组件自动排列在正确的位置?还是我需要重新构建它们?

请帮忙。

谢谢

4

3 回答 3

4
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}

- (void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation {
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {

    // set you subviews,Label button frame here for landscape mode,,
}
else if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
    // set you subviews,Label button frame here for portrait-mode,
}
}

//不要忘记添加这个Delegate方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    // Return YES for supported orientations
    return YES;
}

希望对你有帮助..

于 2012-04-27T04:49:33.443 回答
3

在此处输入图像描述您可以使用 Autosizing 选项进行方向更改。甚至不需要编写一行代码。

于 2012-04-27T04:50:27.457 回答
1

试试这个

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{


    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
       // portrait Arrangement
    }       
    else 
    { 
        // Landscape Arrangement
    }
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
            interfaceOrientation == UIInterfaceOrientationPortrait || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
于 2012-04-27T05:38:53.253 回答