title: Animate UIView on a navigationBar in Swift
body: <p>I´m trying to make an status banner animate on top of a navigationBar.
  The animation works fine if I add the banner on the viewControllers view, but will end up behind the navigation bar. If I add the banner to the navigation bar, the banner "pop" on the navigationBar, but will not preform animation. Same problem if I add the banner view to the keyWindow.
  I also tried to manipulate the banner view and the navigationBars layers zPosition without any luck.
  Anyone hav an idea?</p>
  
  <p>Here is my code...</p>
  
  <pre><code>import UIKit
  
  class BellaBannerView : UIView {
  var view : UIView!
  var style : BannerStyle!
  var position : BannerTopAnchor!
  
  var bannerView : UIView!
  var messageLabel : UILabel!
  var dissmissButton : UIButton!
  var offsetConstraintConstant : CGFloat!
  var testconst : NSLayoutConstraint!
  
  override init(frame: CGRect) {
      super.init(frame: frame)
  }
  convenience init(view: ViewController, style : BannerStyle, pos : BannerTopAnchor) {
  self.init(frame: CGRect.zero)
      self.view = view.view
      self.style = style
      self.position = pos
      self.offsetConstraintConstant = style.bannerSize()
      self.translatesAutoresizingMaskIntoConstraints = false
      self.clipsToBounds = true
  
      if let isNavigation = view.navigationController?.view {
          isNavigation.addSubview(self)
      } else {
          self.view.addSubview(self)
      }
  
      initViews(style: style, pos: pos)
      addSubview(bannerView)
      bannerView.addSubview(dissmissButton)
      bannerView.addSubview(messageLabel)
  
      setConstraints(view: view, pos: pos)
  }
  
  required init?(coder aDecoder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
  }
  
  func initViews(style : BannerStyle, pos : BannerTopAnchor) {
  
      bannerView = {
          let view = UIView()
          view.backgroundColor = style.backgroundColor()
          view.translatesAutoresizingMaskIntoConstraints = false
          return view
      }()
  
      dissmissButton = {
          let btn = UIButton()
          btn.translatesAutoresizingMaskIntoConstraints = false
          btn.backgroundColor = style.backgroundColor()
          btn.setImage(UIImage(named: "dissmiss"), for: .normal)
          btn.addTarget(self, action: #selector(hide), for: .allTouchEvents)
          return btn
      }()
  
      messageLabel = {
          let label = UILabel()
          label.text = "BESKJED beskjed Beskjed"
          label.textColor = .lightGray
          label.textAlignment = .center
          label.translatesAutoresizingMaskIntoConstraints = false
          return label
      }()
  
  }
  
  func show() {
      UIView.animate(withDuration: 0.5, animations: { () -&gt; Void in
          self.testconst.constant = 0
          self.view.layoutIfNeeded()
      })
  }
  
  func hide() {
      UIView.animate(withDuration: 0.5, animations: { () -&gt; Void in
          self.testconst.constant = -self.offsetConstraintConstant
          self.view.layoutIfNeeded()
      })
  }
  
  func setConstraints(view : ViewController, pos : BannerTopAnchor) {
      self.heightAnchor.constraint(equalToConstant: style.bannerSize()).isActive = true
      self.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
      self.topAnchor.constraint(equalTo: self.view.topAnchor, constant: pos.topPosition()).isActive = true
      self.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
  
      bannerView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
      bannerView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
      bannerView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
      self.testconst = bannerView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -self.offsetConstraintConstant)
      self.testconst.isActive = true
  
      dissmissButton.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor).isActive = true
      dissmissButton.trailingAnchor.constraint(equalTo: bannerView.trailingAnchor).isActive = true
      dissmissButton.widthAnchor.constraint(equalToConstant: style.buttonSize()).isActive = true
      dissmissButton.heightAnchor.constraint(equalToConstant: style.buttonSize()).isActive = true
  
      messageLabel.leadingAnchor.constraint(equalTo: bannerView.leadingAnchor, constant: style.buttonSize()).isActive = true
      messageLabel.trailingAnchor.constraint(equalTo: bannerView.trailingAnchor, constant: -style.buttonSize()).isActive = true
      messageLabel.bottomAnchor.constraint(equalTo: bannerView.bottomAnchor).isActive = true
      messageLabel.heightAnchor.constraint(equalToConstant: style.bannerSize()).isActive = true
  }
  
  enum BannerTopAnchor {
      case top
      case statusBar
      case navbar
  
      func topPosition() -&gt; CGFloat {
          switch self {
          case .top:
              return 0
          case .statusBar:
              return UIApplication.shared.statusBarFrame.height
          case .navbar:
              return 64
          }
      }
  }
  
  
  enum BannerStyle {
      case success
      case info
      case offline
      case online
  
      func backgroundColor() -&gt; UIColor {
          switch self {
          case .success, .online:
              return UIColor.green//.bellaSuccessBannerBackground()
          case .info:
              return UIColor.blue//.bellaDarkishBlueColor()
          case .offline:
              return UIColor.red//.bellaLipstickColor()
          }
      }
  
      func bannerSize() -&gt; CGFloat {
          switch self {
          case .info:
              return 50
          case .online, .offline, .success:
              return 25
          }
      }
  
      func buttonSize() -&gt; CGFloat {
          switch self {
          case .info:
              return 50
          default:
             return 0
          }
      }
  
      func textColor() -&gt; UIColor {
          switch self {
          case .success, .online:
              return UIColor.green//bellaDarkishGreenColor()
          case .info:
              return UIColor.brown//bellaBeigeColor()
          case .offline:
              return UIColor.white
          }
      }
  
      func dismissable() -&gt; Bool {
          switch self {
          case .success, .offline, .online:
              return false
          case .info:
              return true
          }
      }
  }
  }
  </code></pre>