简体   繁体   English

如何在iOS中支持横向和纵向视图?

[英]How to support landscape and portrait view in iOS?

In my app i am supporting landscape and portrait orientation for a single ViewController. 在我的应用程序中,我为单个ViewController支持横向和纵向方向。 I can use Autoresize to support both the landscape and portrait. 我可以使用自动调整大小来支持横向和纵向。 But i need to make custom landscape which differs from portrait. 但是我需要制作不同于肖像的自定义风景。 I am very new to iOS. 我是iOS的新手。 Searched a lot in google and SO but couldn't find the solution. 在Google和SO中进行了大量搜索,但找不到解决方案。

I am using Xcode 4.5 and storyboard to make Views. 我正在使用Xcode 4.5和情节提要制作视图。

How to support custom landscape and portrait view? 如何支持自定义横向和纵向视图?

Any help will be much appreciated. 任何帮助都感激不尽。

Try this out in your .m file: 在您的.m文件中尝试一下:

- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation
{
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        // Portrait

        [object setFrame:CGRectMake(...)];

        // Do the same for the rest of your objects
    }

    else
    {
        // Landscape

        [object setFrame:CGRectMake(...)];

        // Do the same for the rest of your objects
    }
}

In the function, you have defined the position of each object in your view, for both Portrait and Landscape. 在该函数中,您已经为“纵向”和“横向”定义了视图中每个对象的位置。

Then you call that function in viewWillAppear to initially make it work; 然后,您可以在viewWillAppear调用该函数以使其最初起作用。 the view determines which orientation to use at start: 该视图确定开始时使用的方向:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self updateLayoutForNewOrientation:self.interfaceOrientation];
}

Also, for when you rotate: 另外,当您旋转时:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration 
{    
     [self updateLayoutForNewOrientation:self.interfaceOrientation];
}

This is the approach I take if I need a more customized look in regard to orientation. 如果我需要有关方向的更多自定义外观,这就是我采用的方法。 Hope that'll work for you. 希望对您有用。

EDIT: 编辑:

If you use two UIViews, one for portrait and another for landscape, in one UIViewController, you would change that first part of the code to look like this: 如果在一个UIViewController中使用两个UIView(一个用于纵向,另一个用于横向),则应将代码的第一部分更改为如下所示:

- (void)updateLayoutForNewOrientation:(UIInterfaceOrientation)orientation
{
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        // Portrait

        portraitView.hidden = NO;
        landscapeView.hidden = YES;
    }

    else
    {
        // Landscape

        portraitView.hidden = YES;
        landscapeView.hidden = NO;
    }
}

There are pros and cons between this edited sample and the original. 此编辑后的样本与原始样本之间存在优缺点。 In the original, you have to code for every object, in this edited sample this code is all you need, however, you need to essentially allocate objects twice, one for a portrait view and another for a landscape view. 最初,您必须为每个对象编写代码,在此编辑后的示例中,此代码就是您所需要的,但是,您实际上需要分配两次对象,一个用于纵向视图,另一个用于横向视图。

YOu can still use Sean's approach but since you have 2 different views you probably have 2 different Xib files so instead of the CGRect section you can do something like [[NSBundle mainBundle] loadNibNamed:"nibname for orientation" owner:self options:nil]; [self viewDidLoad]; 您仍然可以使用Sean的方法,但是由于您有2个不同的视图,因此您可能有2个不同的Xib文件,因此,除了CGRect部分,您可以执行类似[[NSBundle mainBundle] loadNibNamed:"nibname for orientation" owner:self options:nil]; [self viewDidLoad]; [[NSBundle mainBundle] loadNibNamed:"nibname for orientation" owner:self options:nil]; [self viewDidLoad]; . I don't exactly how this would work with storyboards since I haven't used it yet, but I did this in an application that needed a different layout in the orientations so I created 2 Xib files and hooked them both up to the ViewController so on rotation the appropriate Xib file is loaded. 由于尚未使用故事板,因此我不知道该如何使用它,但是我在需要不同方向布局的应用程序中执行了此操作,因此我创建了2个Xib文件并将它们都连接到ViewController,因此旋转时,将加载适当的Xib文件。

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

相关问题 如何使一个视图支持纵向和横向 - How to have one view support portrait and landscape iPhone iOS 6 UITabBarController仅限于纵向,如何使Modal View Controller支持横向方向 - iPhone iOS 6 UITabBarController Portrait Only, How to have Modal View Controller support Landscape Orientation 如何允许特定的视图控制器同时支持横向和纵向 - How to allow a particular view controller to support for both landscape and portrait orientation 如何支持纵向和横向模式 - 使用XIB的可重用视图 - How to support both Portrait and Landscape mode - reusable view with XIBs 支持ios 5(storyboard)的横向和纵向方向 - Support Landscape and Portrait Orientation for ios 5 (storyboard) 如何从横向到纵向查看iOS SDK - How to do a segue from Landscape to Portrait view iOS SDK 如何在iOS中为横向和纵向视图创建单独的布局 - How to Create separate layout for landscape and portrait view in iOS 如何在iOS 6及更高版本中将单个视图从纵向旋转为横向? - How to rotate a single view from portrait to landscape in iOS 6 and above? 在iOS上从横向视图切换到纵向视图 - Switching from Landscape View to Portrait View on iOS iOS方向在特定屏幕上同时支持横向和纵向-并非所有屏幕 - iOS Orientation Support both Landscape and Portrait on specific screen - NOT ALL Screens
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM