仅使用一个.xib文件的纵向和横向的替代iOS布局 [英] Alternative iOS layouts for portrait and landscape using just one .xib file

查看:251
本文介绍了仅使用一个.xib文件的纵向和横向的替代iOS布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xcode和一个.xib文件中使用界面构建器,如何在横向和纵向方向之间旋转时创建备用布局?

Using interface builder in xcode and just one .xib file, how can I create alternate layouts when rotating between landscape and portrait orientations?

参见图表不同的布局

Nb 。绿色视图/区域将包含横向流动的3个项目,在纵向中,这3个项目将在绿色视图/区域内垂直流动。

N.b. the green view/area would contain 3 items flowing horizontal in landscape and in portrait those 3 items would flow vertically within the green view/area.

推荐答案

执行此操作的方法是在.xib文件中包含三个视图。第一个是ViewController的普通视图,没有子视图。

A way to do this is to have three views in your .xib file. The first one is the normal view of your Viewcontroller with no subviews.

然后根据需要为纵向和横向创建视图。所有这三个都必须是根级视图(看一下屏幕截图)

Then you create the views for portrait and landscape as you need them. All three have to be root-level views (have a look at the screenshot)

在Viewcontroller中,创建2个IBOutlet,一个用于纵向,一个用于横向视图,并将它们与界面构建器中的相应视图:

In your Viewcontroller, create 2 IBOutlets, one for the portrait and one for the landscape view and connect them with the corresponding views in the interface builder:

IBOutlet UIView *_portraitView;
IBOutlet UIView *_landscapeView;
UIView *_currentView;

需要第三个视图 _currentView 跟踪当前正在显示这些视图中的哪一个。
然后创建一个这样的新函数:

The third view, _currentView is needed to keep track of which one of these views is currently being displayed. Then create a new function like this:

-(void)setUpViewForOrientation:(UIInterfaceOrientation)orientation
{
    [_currentView removeFromSuperview];
    if(UIInterfaceOrientationIsLandscape(orientation))
    {
        [self.view addSubview:_landscapeView];
        _currentView = _landscapeView;
    }
    else
    {
        [self.view addSubview:_portraitView];
        _currentView = _portraitView;
    }
}

您需要从两个不同的地方调用此函数,首先用于初始化:

You will need to call this function from two different places, first for initialization:

-(void)viewDidLoad
{
    [super viewDidLoad];
    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    [self setUpViewForOrientation:interfaceOrientation];
}

第二个方向更改:

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

希望能帮到你!

这篇关于仅使用一个.xib文件的纵向和横向的替代iOS布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆