ios - 检测 UISplitViewController 何时更改显示模式

标签 ios uisplitviewcontroller

我正在尝试使用 UISplitViewController ,其中辅助 Controller 应在 UISplitViewController 处于并排模式时公开“关闭”功能(通过按钮或按钮栏项目),但在其他时间应隐藏该功能。我试着把它放在辅助 View Controller 中:

override func viewWillAppear(_ animated: Bool) {
    if splitViewController!.primaryHidden {
        // hide the "close" UI artifact
    } else {
        // show the "close" UI artifact
    }
}

这会在第一次显示辅助 View 时正确设置“关闭”函数的可见性,但如果 UISplitViewController 在展开和折叠之间切换(例如,通过旋转 iPhone 6s Plus),则不会再次调用此函数(这是有道理的,因为辅助 Controller 仍然可见)。因此,即使 UISplitViewController 更改模式,“关闭”功能仍保持其初始状态 - 隐藏或显示。

如何让“关闭”功能隐藏或显示以响应 UISplitViewController 模式的变化?

最佳答案

UIViewControllerShowDetailTargetDidChangeNotification通知:

// Sometimes view controllers that are using showViewController:sender and
// showDetailViewController:sender: will need to know when the split view
// controller environment above it has changed. This notification will be 
// posted when that happens (for example, when a split view controller is
// collapsing or expanding). The NSNotification's object will be the view
// controller that caused the change.
UIKIT_EXTERN NSNotificationName const UIViewControllerShowDetailTargetDidChangeNotification NS_AVAILABLE_IOS(8_0);

使用如下
- (void)viewDidLoad{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showDetailTargetDidChange:) name:UIViewControllerShowDetailTargetDidChangeNotification object:self.splitViewController];
}

- (void)showDetailTargetDidChange:(NSNotification *)notification{
    // changed from collapsed to expanded or vice versa
}

此 Apple 示例演示了表格单元格附件如何从纵向显示指示符(表示将发生推送)更改为在更改为横向 Split View时将其删除:
https://developer.apple.com/library/archive/samplecode/AdaptivePhotos/Introduction/Intro.html

请注意,在 iOS 13 beta 上,将 addObserver 与对象 nil 一起使用,因为目前存在一个错误,他们使用错误的对象发送通知。他们使用来自内部类集群的新 UISplitViewControllerPanelImpl 而不是 UISplitViewController 对象。
http://www.openradar.appspot.com/radar?id=4956722791710720

关于ios - 检测 UISplitViewController 何时更改显示模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50183373/

相关文章:

ios - 如何在 ios 10 中控制 CallKit 扬声器按钮

ios - 调整大小时如何调整适合SKSpriteNode内部的子节点?

ios - 如何运行事件指示器直到下载图像完成

ios - UISplitViewController 推送动画故障与 hidesBottomBarWhenPushed

ios - 为什么splitViewController :collapseSecondaryViewController:ontoPrimaryViewController: is not called on iPhone 6 Plus?

iphone - 另一个 UISplitViewController + UITabBarController 问题

ios - 无法在标签中显示经文

ios - 使用 addUIInterruptionMonitor() 清除 XCUITest 中的 SFAuthenticationSession 警报

带有 UISplitViewController 的 iOS 页面转换

ios - 多个 UISplitView 的一个详细 View