How to fade out a titleView label on iOS 12

Viewed 545

I'm trying to fade out a UILabel which has been added as a UIViewController's navigation item's titleView. On iOS 11 and below, the following code correctly fades out the label. When run on iOS 12, the fade in animation works, but the fade out does not animate - the view disappears immediately.

let fadeTextAnimation = CATransition()
fadeTextAnimation.type = .fade
fadeTextAnimation.duration = 0.5

navigationItem.titleView!.layer.add(fadeTextAnimation, forKey: nil)
(navigationItem.titleView as! UILabel).isHidden = didShowNavigationItemTitle

I've also tried changing the code to use UIView.transition, but the same behaviour is observed.

UIView.transition(with: navigationItem.titleView!,
    duration: 0.5,
    options: .transitionCrossDissolve,
    animations: {
        (self.navigationItem.titleView as! UILabel).isHidden = self.didShowNavigationItemTitle
    }, completion: nil)

How can one achieve this fade out in iOS 12?

1 Answers

Just use a UIView.animate block and animate the alpha of the title view.

UIView.animate(withDuration: 0.5, delay: 0, options: .transitionCrossDissolve, animations: {
    self.navigationItem.titleView?.alpha = 0
})
Related