【问题标题】:Set navigations bar height in Swift在 Swift 中设置导航栏高度
【发布时间】:2016-08-11 14:13:00
【问题描述】:

我是 Swift 的新手。我正在尝试更改(增加)导航栏的高度。有什么明显的方法吗? (我尝试使用 CGRectMake 更改它,但它在栏上生成了一个矩形,我丢失了后退按钮等。)哪个是最佳实践?

【问题讨论】:

标签: ios swift height uinavigationbar


【解决方案1】:

子类UINavigationBar也是一种解决办法。

File-New-File- iOS Source - Cocoa Touch 类 - 选择 UINavigationBar 的子类并将其命名为 - CustomNavigationBar。

class CustomNavigationBar: UINavigationBar {

    override init(frame: CGRect) {
        super.init(frame: frame)

//Change here

        self.backgroundColor = UIColor.redColor()

    }
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
    }
    override func drawRect(rect: CGRect) {

//set frame.

    }   
}

【讨论】:

    【解决方案2】:

    我们可以利用继承方法。

    1) 制作 UIViewController 的父控制器。将其命名为 UIAppViewController。

    //  UIAppViewController.swift
    
    import UIKit
    
    class UIAppViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
        }
        override func viewWillAppear(animated: Bool) {
    
        }
        func addCustomNavigationBarToView()
        {
            // Create the navigation bar
            let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 84))
    
            // Create a navigation item with a title
            let navigationItem = UINavigationItem()
            navigationItem.title = "Title"
    
            // Create left and right button for navigation item
            let leftButton =  UIBarButtonItem(title: "Save", style:   UIBarButtonItemStyle.Plain, target: self, action: "saveButtonClicked:")
            let rightButton = UIBarButtonItem(title: "Right", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
    
            // Create two buttons for the navigation item
            navigationItem.leftBarButtonItem = leftButton
            navigationItem.rightBarButtonItem = rightButton
    
            // Assign the navigation item to the navigation bar
            navigationBar.items = [navigationItem]
    
            // Make the navigation bar a subview of the current view controller
            self.view.addSubview(navigationBar)
    
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
    }
    

    请注意,我已经创建了一个将自定义导航栏添加到 self.view 的方法。

    2) 在 ViewController 上:

    //  ViewController.swift
    
    import UIKit
    
    class ViewController: UIAppViewController,UITextFieldDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
        }
        override func viewWillAppear(animated: Bool) {
            self.navigationController?.navigationBarHidden =  false
            self.addCustomNavigationBarToView()
    
        }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      您不应该尝试根据苹果指南更改导航栏的大小。如果需要,您应该使用自定义导航栏。希望这会有所帮助:)

      【讨论】:

        【解决方案4】:

        试试这个代码:

        func sizeThatFits(size: CGSize) -> CGSize 
        {
                var newSize: CGSize = CGSizeMake(self.frame.size.width, 70)
                return newSize
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-15
          • 1970-01-01
          • 2017-09-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-06
          相关资源
          最近更新 更多