【问题标题】:Rotation with Different Layouts - iOS 4不同布局的旋转 - iOS 4
【发布时间】:2011-04-29 00:55:04
【问题描述】:

以下是我希望在我的应用程序中具有的两种布局。如果应用程序从纵向切换到横向时保留 UILabel、BOOL 和其他对象,那就太好了。因为按钮的位置不同,我不能只让纵向视图在自动旋转时自动调整大小。我还想使用 BOOL 和右上角的按钮来实现自己的旋转锁定。

我考虑过将 -(void)orientationChanged:(NSNotification )notificationpresentModalViewController 一起使用,但是这些并没有复制对象并且似乎导致 @987654321 @。

感谢您的帮助!

尝试的解决方案:

我将横向视图添加到 ViewController,在视图控制器中具有两个视图。我将它链接到我在 ViewController 的@interface 部分中添加的 UIView *landscapeView 下的 File's Owner。我将 [self.view addSubview:landscapeView] 添加到 viewDidLoad 方法中。然后我添加了这段代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (orientationLock)
        return NO;
    else {
        if (interfaceOrientation == UIInterfaceOrientationPortrait) {
            [landscapeView setHidden:YES];
            //[self.view setHidden:NO]; removed
        } else {
            //[self.view setHidden:YES]; removed
            [landscapeView setHidden:NO];
        }
        return YES;
    }
}

但这不是正确的解决方案。当我运行模拟器并旋转它时,屏幕没有正确放置。

【问题讨论】:

    标签: objective-c xcode ios4 rotation xib


    【解决方案1】:

    当设备改变方向时,所有事物(您的实例变量值)都保持不变。如果您只有一个 viewController 并且同时显示两个方向,那么您可以轻松管理它。我建议你在 nib 文件中创建两个 UIView,这样你就可以做你想做的所有事情。希望你明白我在说什么。如果你需要帮助,请告诉我。

    这里是修改后的代码

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if (orientationLock)
        return NO;
    else {
        if (interfaceOrientation == UIInterfaceOrientationPortrait) {
            [landscapeView setHidden:YES];
    
        } else {
    
            [landscapeView setHidden:NO];
        }
        return YES;
    }
    }
    

    【讨论】:

    • 我可能需要更多帮助,我是初学者。所以我应该将这两个视图合并到一个 xib 文件中。应用程序如何知道何时使用哪个视图?
    • 是的,当设备改变它的方向时,你会得到一个叫做 shouldRotateToInterfaceOrientation 的方法,你可以检查 interfaceOrientation == UIInterfaceOrientationPortrait 然后你可以显示纵向视图并隐藏横向视图。希望你能理解
    • 您也可以检查标志的值,如果标志的值为是,则仅显示或隐藏,否则一旦标志更改,则不执行任何操作
    • 我试过你说的,但现在屏幕不是纵向模式时是白色的。我觉得我在某个地方犯了一个简单的错误。 (请参阅问题以获取尝试的解决方案)
    • @michaellindahl 确保您将笔尖中的横向 UIView 引用到 IBOutlet。我经常犯这个错误。
    猜你喜欢
    • 2015-06-05
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多