简体   繁体   English

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

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

I have several components on my UIView like textfields, labels, tables, buttons. 我的UIView上有多个组件,例如文本字段,标签,表格,按钮。 I want to handle device orientation changes. 我想处理设备方向更改。 How to make these components arrange at right place automatically? 如何使这些组件自动排列在正确的位置? Or do I need to re-frame them? 还是我需要重新构图?

Please help. 请帮忙。

Thanks 谢谢

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

//Don't forget to add this Delegate Method //不要忘记添加此Delegate方法

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

Hope, this will help you.. 希望这个能对您有所帮助..

在此处输入图片说明 You may use Autosizing option for the orientation changes.No need to write even a single line of code. 您可以使用自动调整大小选项来更改方向,甚至无需编写一行代码。

Try this 尝试这个

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