[英]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.