objective-c - 在 iOS 中使用单独的 View Controller 支持两个方向

标签 objective-c ios cocoa-touch ipad uikit

我现在一团糟。我使用了执行此操作的苹果示例代码:

  1. 创建纵向 View Controller 和横向 View Controller
  2. 然后 Potrait 事件 Controller 注册设备方向更改通知
  3. 当设备旋转时,它会呈现一个用于横向 View 的模态视图 Controller ,或者如果它旋转回纵向 View 则关闭横向 View 。
    除了一个小问题外,一切正常....

现在是我的问题。我用它从 TableView 启动可旋转 View Controller 。它可以旋转并且工作正常。但是,如果我最初以横向模式启动它,它仍将以纵向模式启动。如果我想要横向,我必须在之后再次将它旋转到横向。我非常努力地修复这个但失败了。您可以从Apple Developer Site Here 下载并运行示例代码。 .任何人都可以修复此代码,以便在横向模式下启动时显示横向 View 的模态视图吗?否则我将不得不重写所有内容以使用单个 View Controller 。 这些是苹果代码的相关部分:

- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor colorWithRed:197.0/255.0 green:204.0/255.0 blue:211.0/255.0 alpha:1.0];

LandscapeViewController *viewController = [[LandscapeViewController alloc]
                                                initWithNibName:@"LandscapeView" bundle:nil];
self.landscapeViewController = viewController;
[viewController release];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
                                                name:UIDeviceOrientationDidChangeNotification object:nil];

- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}

- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
    [self presentModalViewController:self.landscapeViewController animated:YES];
    isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
    [self dismissModalViewControllerAnimated:YES];
    isShowingLandscapeView = NO;
}    
}

// override to allow orientations other than the default portrait orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{return (interfaceOrientation == UIInterfaceOrientationPortrait); // support only portrait}

最佳答案

我知道这可能与您不再相关,但我刚刚遇到了同样的故障,这是我的解决方案。

从您设置代码的方式(包括 Apple 的设置方式)

- (void)updateLandscapeView

只有在通知 ViewController 方向发生变化时才会被调用:这里的问题是,这是负责检查其自身方向的方法。 (即启动应用程序不会调用此方法,因此它不会检查设备是否处于任何其他方向)

解决方案非常简单:在启动时强制调用 themethod,即在 viewDidLoad 中。 . .

[self  updateLandscapeView]

这将强制调用该方法并检查接口(interface)方向,第一次后,该方法将在收到更改方向的通知时再次调用

希望这对外面的人有帮助

关于objective-c - 在 iOS 中使用单独的 View Controller 支持两个方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12624910/

相关文章:

ios - 开始开发 iOS 应用程序 (Swift) - 添加按钮不再起作用

ios - UICollectionView header view like table view header

iphone - 对 Objective-C 中复制数组感到困惑

objective-c - 在 iOS 中使用 NSThread 重新触发 CABasicAnimation

objective-c - Cocoapods 问题 : "How does cocoapods know when a new version of a library is available?" etc

ios - 本地址栏中只有前缀时,使 iphone webview 转到不同的网址

ios - 单元测试错误地从启动 View Controller 执行代码

iOS:使用 OutputStream(url: , append:) 发送大文件(超过 3Gb)时出现问题 => 代码状态 500

iphone - 实现 UIViewController 类别

objective-c - 使 Cocoa 窗口全屏显示