Добавить UIView над NavigationBar - PullRequest
0 голосов
/ 28 апреля 2018

Мне нужно поместить UIView между строкой состояния и панелью навигации в Swift.

Я видел много ответов, но ни один из них не помог.

Вот мой код

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

Любая помощь будет оценена

1 Ответ

0 голосов
/ 28 апреля 2018

вы забыли установить рамку вида и метку, сделайте как

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)

вы получаете ОП как

enter image description here

Вариант 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  )
    }

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...