ios - 在 Swift 中检查 UIContentContainer 是否为 ViewContoller

标签 ios swift uiviewcontroller uikit swift3

我正在翻译 Apple 的示例 Custom View Controller Presentations and Transitions (特别是 AAPLCustomPresentationController)到 Swift 中,我遇到了一个问题。

在他们的 preferredContentSizeDidChangeForChildContentContainer 方法(第 190 行)中,他们检查

- (void)preferredContentSizeDidChangeForChildContentContainer:(id<UIContentContainer>)container
{
    [super preferredContentSizeDidChangeForChildContentContainer:container];

    if (container == self.presentedViewController)
        [self.containerView setNeedsLayout];
}

在 Swift (3.0) 中,我尝试过

if container == (self.presentedViewController as UIContentContainer) 

但是我得到了错误

Binary operator '==' cannot be applied to two 'UIContentContainer' operands

我如何在 Swift 中执行此检查?

最佳答案

如果两个对象都是 UIViewController,是否可以在比较之前将它们转换为该类型?

if presentedViewController as? UIViewController == container as? UIViewController {
    // true
}

关于ios - 在 Swift 中检查 UIContentContainer 是否为 ViewContoller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38124906/

相关文章:

ios - 获得 FCM token 后 APNS token 是否仍然有效

iphone - iOS Image View 性能提升建议

swift - 使用按钮在 swift 中切换语句

ios - 如何让我的计算器快速添加句点?

ios - segmentcontrol 没有在 ios swift 中选择

ios - XCTest:如何正确设置 UITextView 的可访问性特征?

ios - 解决中心崩溃报告中的执行顺序错误

iphone - 由于未捕获的异常而终止应用程序 'NSUnknownKeyException' : iOS app crash

iphone - 在 applicationDidBecomeActive 上刷新 ios UIViewController

ios - 为什么在更改演示文稿样式时我的 View 会发生偏移?