objective-c - 带有 ModalPresentationStyle 的 Popover 在 iOS 7 iPad 中不居中

标签 objective-c ipad ios7 modalviewcontroller uimodalpresentationstyle

我在使用 iOS 7 时遇到了一个问题,这似乎是一个错误,或者我只是没有做正确的事情。我有 modalViewController,它在 iPad 上显示为带有 ModalPresentationStyle 的弹出窗口。而且它不是标准尺寸,定制尺寸。 这是代码:

myViewController *myVC = [[myViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myVC];
[nav setModalPresentationStyle:UIModalPresentationFormSheet];
[nav setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];
[self presentViewController:nav animated:YES completion:nil];
nav.view.superview.bounds = CGRectMake(0, 0, 320, 465);

在 iOS 6 中一切正常,但在 iOS 7 中它不居中。 但是,如果我将 ModalTransitionStyle 设置为 UIModalTransitionStyleCrossDissolve,它就可以正常工作。但仅限于这种模式。 也许有人也偶然发现了这个并且知道如何修复它?我不太喜欢溶解效果。 谢谢。

最佳答案

我遇到了同样的问题。我已经通过使用另一种方法解决了这个问题,发现 here .

此解决方案建议使用方法 (void)viewWillLayoutSubviews

因此,如果是@Manuel M.,请在 GeneralSettingsViewController 中添加以下代码:

// GeneralSettingsViewController
- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 497, 375);
}

您将不再需要此代码:

self.generalSettingsVC.view.superview.frame = CGRectMake(0, 0, 497, 375);
self.generalSettingsVC.view.superview.center = self.view.center;

对于@titicaca,您使用的是 UINavigationController 我还没有用这个 Controller 测试它,但您可以尝试我提到的相同解决方案,扩展 UINavigationController 并覆盖viewWillLayoutSubviews 方法。

[编辑]

对于@titicaca,我在一个新项目中尝试了它并且对我来说它有效。我所做的是使用自定义导航 View Controller CustomNavigationController 覆盖 viewWillLayoutSubviews,如下所示:

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.view.superview.bounds = CGRectMake(0, 0, 330, 284);
}

然后,呈现 CustomNavigationController 的 View Controller 应该执行类似于以下的代码:

UIViewController *myVC = [[UIViewController alloc] init];
[myVC.view setBackgroundColor:[UIColor redColor]];

CustomNavigationController *nav = [[CustomNavigationController alloc] initWithRootViewController:myVC];
[nav setModalPresentationStyle:UIModalPresentationFormSheet];
[nav setModalTransitionStyle: UIModalTransitionStyleFlipHorizontal];

[self presentViewController:nav animated:YES completion:nil];

不过你需要确保 self.view.superview.bounds = CGRectMake(0, 0, 330, 284); 的尺寸是偶数,否则里面的文字会变得模糊, 如果有的话

关于objective-c - 带有 ModalPresentationStyle 的 Popover 在 iOS 7 iPad 中不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18940424/

相关文章:

ios - 获取UITextView中链接的文本范围

iphone - 如何在特定 View 中隐藏标签栏?

iPhone 和 iPad - 可以在 Xcode 上更改产品名称吗?

ios - 如何在 ios 中读取图像的完整 exif 和 tiff 信息

iOS7 条码扫描器 API 添加零到 UPCA 条码格式

objective-c - NSIntegers 的 NSArray 类别的快速枚举

ios - 如何在其他类 objective-c 中获取循环值

iphone - UIWebView Fontsize 和 UItextView Fontsize 之间的区别

在 iPad 上滚动时 JavaScript 动画暂停

iOS : 3. 4 : App names in iTunes Connect and as displayed on a device should be similar, 以免造成混淆