在我的应用程序中,我支持单个ViewController的横向和纵向。我可以使用Autoresize来支持横向和纵向。但我需要制作与人像不同的自定义风景。我对iOS很新。在谷歌和搜索了很多搜索,但无法找到解决方案。如何在iOS中支持横向和纵向视图?
我正在使用Xcode 4.5和故事板使视图。
如何支持自定义横向和纵向视图?
任何帮助将不胜感激。
在我的应用程序中,我支持单个ViewController的横向和纵向。我可以使用Autoresize来支持横向和纵向。但我需要制作与人像不同的自定义风景。我对iOS很新。在谷歌和搜索了很多搜索,但无法找到解决方案。如何在iOS中支持横向和纵向视图?
我正在使用Xcode 4.5和故事板使视图。
如何支持自定义横向和纵向视图?
任何帮助将不胜感激。
在.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
}
}
在功能方面,已定义的每个对象的位置在您看来,对于人像和风景。
然后你在viewWillAppear
中调用该函数来初始化它的工作;视图确定在开始使用哪个方向:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self updateLayoutForNewOrientation:self.interfaceOrientation];
}
而且,当你旋转:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
[self updateLayoutForNewOrientation:self.interfaceOrientation];
}
这是我采取的办法,如果我需要考虑的方向更加定制的外观。希望这会为你工作。
编辑:
如果您使用两个UIViews,一个纵向和另一景观,在一个UIViewController中,你会改变的代码是第一部分看起来像这样:
- (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;
}
}
有这个编辑过的样本和原始文本之间的优缺点。在原始代码中,您必须为每个对象编码,在此编辑的示例中,此代码是您需要的全部代码,但是,您需要基本上分配对象两次,一次是纵向视图,另一次是横向视图。
你仍然可以使用肖恩的方法,但由于你有2个不同的视图,你可能有2个不同的Xib文件,所以,而不是CGRect部分,你可以做一些像[[NSBundle mainBundle] loadNibNamed:"nibname for orientation" owner:self options:nil]; [self viewDidLoad];
。我不完全知道这将如何与故事板,因为我还没有使用它,但我在一个需要在方向不同的布局的应用程序中做到了这一点,所以我创建了2个Xib文件并将它们都连接到了ViewController上在旋转时加载适当的Xib文件。
感谢您的回答。我有点困惑CGRectMake中发生了什么。因为目前我使用故事板制作了两个视图。一个用于横向和其他一个肖像。如何将这两个视图链接到您的代码中 – GoCrazy
CGRectMake用于定义接口对象的位置,除了这些对象的大小 - CGRectMake(x位置,y位置,宽度,高度)' - - 我会编辑我的答案,以反映如果您使用两个UIViews会做什么。 – Scott
谢谢肖恩,这将是优秀的。很多赞赏 – GoCrazy