【问题标题】:How to support landscape and portrait view in iOS?如何在 iOS 中支持横向和纵向视图?
【发布时间】:2023-03-12 07:24:01
【问题描述】:

在我的应用程序中,我支持单个 ViewController 的横向和纵向。我可以使用 Autoresize 来支持横向和纵向。但我需要制作不同于肖像的自定义风景。我对 iOS 很陌生。在 google 和 SO 中搜索了很多,但找不到解决方案。

我正在使用 Xcode 4.5 和 storyboard 来制作视图。

如何支持自定义横向和纵向视图?

任何帮助将不胜感激。

【问题讨论】:

    标签: iphone ios uiviewcontroller landscape


    【解决方案1】:

    在你的 .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];
    }
    

    如果我需要在方向方面更个性化的外观,我会采用这种方法。希望这对你有用。

    编辑:

    如果您在一个 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;
        }
    }
    

    这个经过编辑的样本与原始样本各有利弊。在原始版本中,您必须为每个对象编写代码,在此编辑后的示例中,您只需要此代码,但是,您实际上需要分配对象两次,一次用于纵向视图,另一次用于横向视图。

    【讨论】:

    • 感谢您的回答。我对 CGRectMake 中的内容有点困惑。因为目前我使用情节提要制作了两个视图。一个用于横向,另一个用于纵向。如何将这两个视图链接到您的代码中
    • CGRectMake 用于定义接口对象的位置,以及这些对象的大小 -- CGRectMake(x-position, y-position, width, height) -- 我将编辑我的答案以反映如果你是你会做什么使用两个 UIView。
    • 如果我的附加代码是您要查找的内容,您可能还希望在 updateLayout 函数中包含[landscapeView setFrame:CGRectMake(0, 0, width, height)] 等,甚至可能是 viewDidLoad,以防某些东西在接口中意外重定位生成器。
    • 感谢您的详细解答。我今天不能尝试,因为我现在没有 Mac。我会在星期一试试这个,让你知道 +1 为你的努力和支持干杯
    • 你能解释一下吗?我不知道我是怎么做到的,但我需要使用这个方向,请帮助我..
    【解决方案2】:

    您仍然可以使用 Sean 的方法,但由于您有 2 个不同的视图,您可能有 2 个不同的 Xib 文件,因此您可以执行 [[NSBundle mainBundle] loadNibNamed:"nibname for orientation" owner:self options:nil]; [self viewDidLoad]; 之类的操作,而不是 CGRect 部分。因为我还没有使用它,所以我不知道这将如何与故事板一起使用,但我在一个需要不同方向布局的应用程序中执行此操作,因此我创建了 2 个 Xib 文件并将它们都连接到 ViewController 所以旋转时会加载相应的 Xib 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 2018-10-11
      相关资源
      最近更新 更多