代码之家  ›  专栏  ›  技术社区  ›  Chandler Long

Swift 4:无法通过编程更改导航栏上的高度

  •  0
  • Chandler Long  · 技术社区  · 7 年前

    我有一个为ViewController设置的自定义导航栏。问题是导航栏太小,我无法更改它的高度。

    class AddGameViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = Color.lightGrey
        self.setNavigationBar()
    
    }
    func setNavigationBar() {
        let screenSize: CGRect = UIScreen.main.bounds
        let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: screenSize.width, height: 50))
        let navItem = UINavigationItem(title: "Create Game")
        let doneItem = UIBarButtonItem(image: UIImage(named: "Cancel"), style: .plain, target: self, action: #selector(cancelGameCreation))
        navItem.leftBarButtonItem = doneItem
        navBar.setItems([navItem], animated: false)
        self.view.addSubview(navBar)
    }
    
    @objc func cancelGameCreation() {
        dismiss(animated: true, completion: nil)
        }
    }
    

    No matter what my height is set to. This is the appearance of my Navigation bar

    2 回复  |  直到 7 年前
        1
  •  1
  •   Steve    7 年前
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = UIColor.lightGray
            self.setNavigationBar()  
        }
    
        func setNavigationBar() {
            let navBar = UINavigationBar()
            navBar.translatesAutoresizingMaskIntoConstraints = false
            let navItem = UINavigationItem(title: "Create Game")
            let doneItem = UIBarButtonItem(image: UIImage(named: "Cancel"), style: .plain, target: self, action: #selector(cancelGameCreation))
            navItem.leftBarButtonItem = doneItem
            navBar.setItems([navItem], animated: false)
            self.view.addSubview(navBar)
            if #available(iOS 11, *) {
                let guide = view.safeAreaLayoutGuide
                navBar.topAnchor.constraint(equalTo: guide.topAnchor).isActive = true
            } else {
                navBar.topAnchor.constraint(equalTo: topLayoutGuide.bottomAnchor).isActive = true
            }
            navBar.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
            navBar.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
            navBar.heightAnchor.constraint(equalToConstant: 600).isActive = true
        }
    
        @objc func cancelGameCreation() {
            dismiss(animated: true, completion: nil)
        }
    }
    

    使用锚定,您可以将创建的导航固定到状态栏的底部,如Tommi所建议的那样。

    尽管如此,我并没有看到您在上面展示的设计中有多少无法通过调整导航控制器上导航栏的外观来处理的问题。您决定构建自己的导航栏,而不是将视图控制器添加到导航控制器中,并在导航栏上设置栏按钮项目和标题,这有什么原因吗。导航控制器应该处理您看到的约束问题。

    假设将视图控制器添加到导航控制器,一个简单的例子如下:(只是一个想法,希望这有帮助)

    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            view.backgroundColor = UIColor.lightGray
            navigationItem.title = "Create Game"
            let doneItem = UIBarButtonItem(image: UIImage(named: "Cancel"), style: .plain, target: self, action: #selector(cancelGameCreation))
            navigationItem.setLeftBarButton(doneItem, animated: false)
            navigationController?.navigationBar.barTintColor = UIColor.blue
       }
    
        @objc func cancelGameCreation() {
            dismiss(animated: true, completion: nil)
        }
    }
    
        2
  •  0
  •   Tommi Kivimäki    7 年前

    导航栏具有固定高度。您似乎将其放置在状态栏的顶部。尝试使用约束将导航栏的顶部固定到状态栏的底部。