2
  • I want to keep the UIView always on top (not the top of the ViewController) throughout the entire app like youtube player???
  • I want to make this UIView draggable to the screen? enter image description here
Akbar Khan
  • 2,215
  • 19
  • 27

2 Answers2

0

you can use the following code to add a view to your application window

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    // Do any additional setup after loading the view, typically from a nib.
    let newView = UIView()
    newView.frame = CGRect(x:0,y:UIApplication.shared.statusBarFrame.height, width: view.frame.size.width, height: 30)
    newView.backgroundColor = UIColor.red
    let label = UILabel()
    label.frame = newView.bounds
    label.text = "header"
    newView.addSubview(label)
    UIApplication.shared.keyWindow?.addSubview(newView)

    let bounds = self.navigationController!.navigationBar.bounds
    self.navigationController?.navigationBar.frame = CGRect(x: 0, y: newView.frame.size.height + newView.frame.origin.y, width: bounds.width, height: bounds.height  )
}

Please check and let me know if this works

Kuldeep
  • 4,466
  • 8
  • 32
  • 59
Parth Dhorda
  • 712
  • 1
  • 8
  • 32
  • this worked but there is a problem, when i dismiss the view controller and navigation controller then everything get stopped. This means i lost all the references of view controller. – Akbar Khan Jan 09 '19 at 07:10
  • Try to use single navigation controller from the AppDelegate – Parth Dhorda Jan 09 '19 at 08:38
0

UIApplication.shared.keyWindow?.addSubview(UIView)

Arjun Patel
  • 1,394
  • 14
  • 23