ios - swift 4 : Unable to programmatically change the height on my Navigation Bar

标签 ios swift uinavigationbar

我有一个为我的 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

最佳答案

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

使用 anchor ,您可以按照 Tommi 上面的建议将您创建的导航固定到状态栏的底部。

话虽这么说,但在您上面展示的设计中,我并没有看到太多无法通过调整导航 Controller 上的导航栏外观来处理的问题。您决定构建自己的导航栏而不是将 View Controller 添加到导航 Controller 并设置栏按钮项和该导航栏上的标题是否有原因。导航 Controller 应该为您处理您看到的约束问题。

假设将 View Controller 添加到导航 Controller 的快速示例如下所示:(只是一个想法,希望这会有所帮助)

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

关于ios - swift 4 : Unable to programmatically change the height on my Navigation Bar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48967030/

相关文章:

ios - UITextView 改变高度从纵向到横向

ios - UICollectionView 多个单元格选择 Swift iOS

swift - 添加嵌套字典导致 JSONSerialization 返回 nil

swift - Swift Tensorflow 中的#tfop 是什么,它在哪里定义?

ios - Controller navigationItem.leftBarButtonItem 在 IOS7 中改变颜色

iphone - 将带有两个 UIBarButtonItem 的 UIToolbar 添加到 UINavigationBar : poor UIToolbar and what about iPhone 4

ios - UINavigationBar 剪辑顶部内容

ios - 日历:获取日期组件,奇怪的情况

ios - Xcode 6.2 beta 4 是否让 watchKit 接口(interface)与页面无法打开推送接口(interface)?

ios - 如何通过传递 Firebase 引用来跟踪用户?