ios – Swift自定义导航控制器类

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

《ios – Swift自定义导航控制器类》

我们可以看到,这个导航控制器有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


    }
}

它给出了这个结果:
《ios – Swift自定义导航控制器类》
你可以看到它没有图片.我知道如果我添加此代码:

        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中),它将使用按钮,图像等制作一个漂亮的导航控制器.此外,我可以创建一个不同的类另一个导航控制器并将其连接到不同的屏幕,例如,我不需要图像,只需按钮.所以问题是:如何制作自定义导航控制器类?

我会很高兴并感谢任何帮助,谢谢!

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

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%有效!谢谢!

点赞