jjatie jjatie - 5 months ago 71
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?

JAL JAL
Answer

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

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