Swift自定义导航控制器类

时间:2017-02-18 08:23:10

标签: ios uinavigationcontroller swift3 uinavigationbar uinavigationitem

每个读它的人,你好,谢谢你。我正在使用Xcode 8.2.1和Swift 3.0为iOS设备编写Viber或WhatsApp等应用程序。我有一个问题:我希望我的应用程序有一个导航控制器,但不是一个简单的,我希望它是一个自定义的。 WhatsApp有一个打印屏幕可以理解,我想要实现的目标:

Screenshot of WhatsApp chat with somebody

正如我们所看到的,这个导航控制器有3个按钮,标签和图像与头像。我也想将我的颜色改为绿色。由于这样做,我创建了类:

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


    }
}

它给出了这个结果: enter image description here 你可以看到它没有图片。我知道如果我添加这段代码:

        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,带有徽标的图像会出现,但我想要完整的类,我将在某处连接(例如,在Interface Builder中),它将制作一个漂亮的导航控制器,带有按钮,图像等。此外,我可以创建一个与另一个导航控制器不同的类,并将其连接到不同的屏幕,例如,我不需要图像,只有按钮。所以问题是:如何制作自定义导航控制器类?

我会非常高兴和感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:4)

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

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

}

现在,您可以从此类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%可行!谢谢!