【问题标题】:iOS: Autolayout and popOverController issue (possible Xcode bug)iOS:Autolayout 和 popOverController 问题(可能是 Xcode 错误)
【发布时间】:2023-03-03 03:46:01
【问题描述】:

我在下面的代码中显示了我的popOverController 用于 iPhone 和 iPad。

 ViewController *vc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];

if ([UIDevice currentDevice].userInterfaceIdiom ==
    UIUserInterfaceIdiomPad) {
    vc.preferredContentSize = CGSizeMake(296, 476); //your custom size.
} else {
    vc.preferredContentSize = CGSizeMake(252, 436.5);
}

 vc.modalPresentationStyle = UIModalPresentationPopover;
 vc.popoverPresentationController.delegate = self;
 vc.popoverPresentationController.sourceView = self.view;
 vc.popoverPresentationController.sourceRect = CGRectMake(screenWidth / 2, self.view.height / 2, 1, 1);

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

UIPopoverPresentationController *popOverController =  vc.popoverPresentationController;
popOverController.permittedArrowDirections = 0;

但是,似乎出现了班级规模问题。即使在运行 iPad 设备时,似乎使用的是 iPhone 尺寸,而不是 iPad 尺寸。尽管在界面构建器中,它会根据所选设备显示两种不同大小的按钮。

编辑:例如,我有一个带有按钮的 viewcontroller.xib。根据实际设备,有两种不同的按钮尺寸。在 iPhone 上,按钮的尺寸为 50 x 50。在 iPad 上,它的尺寸为 430 x 430。在界面生成器中,这显示正确。但是,在运行 iPad 设备时,按钮显示为 50 x 50,而假设为 430 x 430。

【问题讨论】:

  • “大小”是什么意思?在 iPhone 上,你甚至不会看到弹出框;您将看到一个全屏显示的视图控制器。那么你怎么知道“大小”应该是什么?请显示屏幕截图。
  • 例如,我有一个带有按钮的 viewcontroller.xib。根据实际设备,有两种不同的按钮尺寸。在 iPhone 上,按钮的尺寸为 50 x 50。在 iPad 上,它的尺寸为 430 x 430。在界面生成器中,这显示正确。但是,在运行 iPad 设备时,按钮显示为 50 x 50,而假设为 430 x 430。而且我在 iPhone 上也有一个弹出窗口。
  • 好的,我明白了。

标签: ios objective-c autolayout xib popover


【解决方案1】:

情节提要中的条件约束使用大小类

iPad 上弹出式视图控制器上的水平尺寸类是 Compact,就像在 iPhone 上一样。这就是为什么您会看到按钮的 iPhone 尺寸。

要确认这是真的,请将 ViewController 类中的 viewDidAppear: 覆盖为 NSLog self.traitCollection。您会看到水平尺寸类是 Compact。

这是有道理的,因为弹出框就像出现在 iPad 上的 iPhone 大小的窗口。事实上,这就是弹出框的全部

【讨论】:

  • 有没有办法为 iPhone 和 iPad 提供两个不同大小的按钮,而无需使用多个 xib 文件(我正在使用 xib)。另外,我会奖励你正确的答案。
  • 是的,我想。您可以告诉演示控制器对弹出框说谎,并告诉它特征集合具有常规宽度。 developer.apple.com/reference/uikit/uipresentationcontroller/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-04
  • 2015-03-29
  • 2011-07-22
  • 1970-01-01
相关资源
最近更新 更多