繁体   English   中英

当iPhone / iPad的设备方向改变时,如何处理UI组件?

[英]How to handle UI components when device orientation for iPhone/iPad changes?

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

请帮忙。

谢谢

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

希望这个能对您有所帮助..

在此处输入图片说明 您可以使用自动调整大小选项来更改方向,甚至无需编写一行代码。

尝试这个

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{


    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
       // portrait Arrangement
    }       
    else 
    { 
        // Landscape Arrangement
    }
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || 
            interfaceOrientation == UIInterfaceOrientationPortrait || 
            interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
            interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM