Ios 在导航栏上方添加UIView

Ios 在导航栏上方添加UIView,ios,swift,uinavigationbar,Ios,Swift,Uinavigationbar,我需要在Swift中的状态栏和导航栏之间放置一个UIView 我看到了很多答案,但似乎没有一个是有效的 这是我的密码 let newView = UIView() let label = UILabel() label.text = "header" newView.addSubview(label) UIApplication.shared.keyWindow?.addSubview(newView) let rootVC: HomeVC =

我需要在Swift中的状态栏和导航栏之间放置一个UIView

我看到了很多答案,但似乎没有一个是有效的

这是我的密码

    let newView = UIView()
    let label = UILabel()
    label.text = "header"
    newView.addSubview(label)
    UIApplication.shared.keyWindow?.addSubview(newView)

    let rootVC: HomeVC = UIStoryboard(.Home).instantiateViewController()
    navController = UINavigationController(rootViewController: rootVC)
    self.window?.rootViewController = self.navController
    self.window?.makeKeyAndVisible()

任何帮助都将不胜感激

您忘记设置视图框架和标签了,您喜欢吗

let newView = UIView()
    newView.frame = CGRect(x:0,y:UIApplication.shared.statusBarFrame.height, width: view.frame.size.width, height: self.navigationController!.navigationBar.frame.height)
    newView.backgroundColor = UIColor.red
    let label = UILabel()
    label.frame = newView.bounds
    label.text = "header"
    newView.addSubview(label)
    UIApplication.shared.keyWindow?.addSubview(newView)
你得到OP as吗

选项2

根据您的要求

    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  )
    }

您忘了设置视图框架和标签,您喜欢吗

let newView = UIView()
    newView.frame = CGRect(x:0,y:UIApplication.shared.statusBarFrame.height, width: view.frame.size.width, height: self.navigationController!.navigationBar.frame.height)
    newView.backgroundColor = UIColor.red
    let label = UILabel()
    label.frame = newView.bounds
    label.text = "header"
    newView.addSubview(label)
    UIApplication.shared.keyWindow?.addSubview(newView)
你得到OP as吗

选项2

根据您的要求

    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  )
    }

谢谢Anbu,我想要导航栏和newView。我试过你的代码,但没用。我需要新视图下面的导航栏。我可以给你举一个我需要的例子@ParthDhorda-给我更新答案的时刻@Anbu.karthik,谢谢你的帮助它工作了,但是导航栏的高度太小了你好@Anbu。我今天在真实设备上进行测试,导航栏移到顶部,并位于我在KeyWindow中添加的视图下方。感谢Anbu,我希望导航栏也与newView一起使用。我试过你的代码,但没用。我需要新视图下面的导航栏。我可以给你举一个我需要的例子@ParthDhorda-给我更新答案的时刻@Anbu.karthik,谢谢你的帮助它工作了,但是导航栏的高度太小了你好@Anbu。我今天在真实的设备上进行测试,导航栏移到顶部,并位于我在keyWindow中添加的视图下方