问题描述
我正在为视图控制器转到基于 UIPresentationController
的演示文稿,但是与API产生了一些混淆.
I'm moving over to UIPresentationController
based presentations for my view controllers but have run into some confusion with the API.
我有一个自定义的侧边栏样式视图控制器演示文稿(类似于 LookInside
WWDC 2014演示代码).
I have a custom sidebar style view controller presentation (similar to the LookInside
WWDC 2014 demo code).
该类集群( UIPresentationController
, UIViewControllerTransitioningDelegate
和 UIViewControllerAnimatedTransitioning
)将视图控制器作为常规工具从屏幕边缘显示为侧边栏尺寸类视图,并在紧凑尺寸类视图上呈现与全屏相同的视图控制器.
This class cluster (UIPresentationController
, UIViewControllerTransitioningDelegate
, and UIViewControllerAnimatedTransitioning
) presents a view controller as a sidebar from the edge of the screen on regular size class views, and presents the same view controller as full screen on compact size class views.
在可调整大小的iPad目标上进行测试可以显示正确的行为:我将水平尺寸类别设置为紧凑",并且我的视图控制器从侧边栏切换到全屏.
Testing this on the Resizable iPad target shows the correct behaviour: I set the horizontal size class to "Compact" and my view controller switches from sidebar to full screen.
但是,我想要更多的粒度.当设备处于横向时,我想在iPhone 6和6+上使用侧边栏样式的视图控制器演示,并为所有纵向的iPhone使用全屏风格的演示.
However, I want more granularity. I would like to use the sidebar-style view controller presentation on iPhone 6 and 6+ when the device is in landscape orientation, and use the full-screen style presentation for all iPhones in portrait orientation.
所以在我的方法中
- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
我实现了一些逻辑,以检测侧边栏是否会占据屏幕的太多位置,假设我使用以下条件:
I implemented some logic to detect whether the sidebar will occupy too much of the screen, let's say I use the following condition:
//If my sidebar is going to occupy more than half the new width of the view...
if( self.sidebarTransitionController.width > size.width / 2.0 )
{
//Override the presentation controller's trait collection with Compact horizontal size class
sidebarPresentationController.overrideTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
}
else
{
//Otherwise override the trait collection with Regular
sidebarPresentationController.overrideTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassRegular];
}
但是这什么也没做. UIPresentationController.overrideTraitCollection
的文档指出:
However this does nothing. The documentation for UIPresentationController.overrideTraitCollection
states:
为此属性分配一个新值会导致表示控制器转换到新的特征集,这可能会导致动画出现在所显示的界面中.
Assigning a new value to this property causes the presentation controller to transition to the new set of traits, which could result in animations to the presented interface.
为演示控制器分配新值不会导致我的演示界面发生任何变化.(即使从 UIViewControllerTransitioningDelegate
对象内部创建 UIPresentationController
时,即使我分配了 overrideTraitCollection
.)
Assigning the new value to the presentation controller does not cause my presented interface to change in any way. (Even if I assign the overrideTraitCollection
when the UIPresentationController
is created from within the UIViewControllerTransitioningDelegate
object.)
我想念什么?可以使用 UIPresentationController
在更细粒度的级别上执行自适应演示吗?
What am I missing? Is it possible to perform adaptive presentation with UIPresentationController
on a more granular level?
推荐答案
不容易.
我建议以下选项之一:
-
放弃控制并接受UIKit的有限适应性:您可以更改为全屏演示,也可以为特定的特征集呈现其他视图控制器.与此配合使用,可以更快地发布您的应用.
Give up on control and accept UIKit’s limited adaptivity: you can change to a full screen presentation or present a different view controller for a particular trait collection. Go with this to ship your app faster.
使用演示文稿,但可以使用UIKit.一种方法是重写 viewWillTransitionToSize:withTransitionCoordinator:
并关闭然后重新呈现所呈现的视图控制器,进行所需的任何更改,例如提供不同的呈现样式或呈现控制器.无需花费太多时间就可以得到很好的结果.
Use presentations but work against UIKit. One way is to override viewWillTransitionToSize:withTransitionCoordinator:
and dismiss and then re-present the presented view controller, making any changes you want such as providing a different presentation style or presentation controller. This could give okay results without taking too much time.
使用视图控制器容器.这是遵循UIKit最佳实践时可以走到的最低级别.您的主视图控制器将成为容器视图控制器的子代,而不是要求您显示该容器以显示另一个视图控制器.如果该应用程序应自定义且精美,则可以使用它,您可以花一些时间使它变得恰到好处.
Use view controller containment. This is about the lowest level you can go while sticking with UIKit best practices. Your main view controller becomes a child of a container view controller, and instead of presenting you ask the container to show the other view controller. Go with this if the app should be custom and exquisite, and you can spend the time to make it just right.
这篇关于基于视图大小的自适应UIPresentationController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!