Swift 自定义导航控制器 Class

Swift Custom Navigation Controller Class

各位看书的朋友,您好,谢谢。我正在使用 Xcode 8.2.1 和 Swift 3.0 为 iOS 设备编写类似 Viber 或 WhatsApp 的应用程序。我有一个问题:我希望我的应用程序有一个导航控制器,但不是一个简单的,我希望它是一个自定义的。有一个来自 WhatsApp 的打印屏幕需要理解,我正在努力实现的目标:

正如我们所见,这个导航控制器有 3 个按钮、标签和带有头像的图像。我也想把我的颜色改成绿色。为此,我创建了 class:

import UIKit

class CustomNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationBar.barTintColor = UIColor.green


        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 39, height: 39))
        imageView.contentMode = .scaleAspectFit
        let image = UIImage(named: "logo")
        imageView.image = image
        navigationItem.titleView = imageView


    }
}

它给出了这个结果: 如您所见,它没有图片。我知道如果我添加这段代码:

        let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 39, height: 39))
        imageView.contentMode = .scaleAspectFit
        let image = UIImage(named: "logo")
        imageView.image = image
        navigationItem.titleView = imageView

到 ViewController,带有徽标的图像会出现,但我想要完整的 class,我将把它连接到某个地方(例如,在 Interface Builder 中),它将制作一个漂亮的带按钮的导航控制器,图片等。此外,我可以用另一个导航控制器创建一个不同的 class 并将其连接到不同的屏幕,例如,我不需要图像,只需要按钮。所以问题是:如何制作自定义导航控制器 class?

如果有任何帮助,我将非常高兴和感激,谢谢!

现在,我知道答案了!它将为 Swift 4.1.2 编写。从 UIViewController 继承的每个 class 都有一个 属性 self.navigationController?.navigationBarnavigationItem。您可以像这样创建自己的 class:

class CustomNavigationController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let color = UIColor(red: 81 / 255, green: 155 / 255, blue: 22 / 255, alpha: 1.0)
        self.navigationController?.navigationBar.barTintColor = color

        let image = UIImage(named: "logo")
        let imageView = UIImageView(image: image)
        imageView.contentMode = .scaleAspectFit
        navigationItem.titleView = imageView
    }

    private func imageView(imageName: String) -> UIImageView {
        let logo = UIImage(named: imageName)
        let logoImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 35, height: 35))
        logoImageView.contentMode = .scaleAspectFit
        logoImageView.image = logo
        logoImageView.widthAnchor.constraint(equalToConstant: 35).isActive = true
        logoImageView.heightAnchor.constraint(equalToConstant: 35).isActive = true
        return logoImageView
    }

    func barImageView(imageName: String) -> UIBarButtonItem {
        return UIBarButtonItem(customView: imageView(imageName: imageName))
    }

    func barButton(imageName: String, selector: Selector) -> UIBarButtonItem {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: imageName), for: .normal)
        button.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
        button.widthAnchor.constraint(equalToConstant: 35).isActive = true
        button.heightAnchor.constraint(equalToConstant: 35).isActive = true
        button.addTarget(self, action: selector, for: .touchUpInside)
        return UIBarButtonItem(customView: button)
    }

}

现在您可以从这个 class CustomNavigationController 继承任何 UIViewControllers 并根据需要添加大量按钮、图标、更改颜色、徽标等(当然,在普通范围)!像这样:

class ViewController: CustomNavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.leftBarButtonItem = barImageView(imageName: "picture1")

        let firstImage = barImageView(imageName: "picture2")
        let secondButton = barButton(imageName: "picture3", selector: #selector(secondButtonPressed))

        navigationItem.rightBarButtonItems = [firstImage, secondButton]
    }

    @objc func secondButtonPressed() {
        print("Pressed")
    }

}

这是我的解决方案,也许不是最好的,但 100% 有效!谢谢!