jjatie jjatie - 1 year ago 126
iOS Question

Check if UIContentContainer is ViewContoller in Swift

I am translating Apple's sample Custom View Controller Presentations and Transitions (specifically the AAPLCustomPresentationController) into Swift, and I've stumbled into a problem.

In their preferredContentSizeDidChangeForChildContentContainer method (line 190) they check

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

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

In Swift (3.0), I tried

if container == (self.presentedViewController as UIContentContainer)

But I get the error

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

How can I perform this check in Swift?

Answer Source

If both objects are UIViewControllers, maybe cast them as that type before comparing?

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