自定义 UINavigationItem class - 标题取决于活动的 UIStoryboard

Custom UINavigationItem class - Title dependent on Active UIStoryboard

我创建了一个包含三个故事板的应用程序。 (&多个 VC's)我正在创建一个全局 NavigationItem class,我可以将其输入故事板,因此每个视图控制器都继承相同的信息,我只需要编写一页代码而不是复制和粘贴进入每个 VC。

我想以编程方式设置标题。标题可以根据活动的情节提要和大小 phone 的大小而变化。即完整标题或 shorthand 如果它显示在 iPhone SE 上。

我不会用。 UIStoryboard 作为 bool,因为它不符合。我如何知道我在哪个故事板中,以便它可以在 if 语句中设置标题?我写的代码是错误的,不适用于故事板。

到目前为止,我的 NavigationItemCustom class 代码设置如下 我发现很难找到我所在的故事板?谁能把我推向正确的方向,拜托。我的代码如下:

import UIKit
class NavigationBarSetUp: UINavigationItem {

    override func awakeFromNib() {
        super.awakeFromNib()


        let currentstoryboard = UIStoryboard()
        let screenWidth = UIScreen.main.bounds.width

        let storyboard1 = UIStoryboard(name: "First", bundle: nil)
        //let storyboard2 = UIStoryboard(name: "Second", bundle: nil)
        //let storyboard3 = UIStoryboard(name: "Third", bundle: nil)

        //Current title
        self.title = nil

        if currentstoryboard == storyboard1 && (screenWidth == 320) {

            self.title = " diet 1"
            print ("called short title")

        } else if currentstoryboard == storyboard1 && (screenWidth > 320) {

            // rest of screen sizes
            self.title = " Welcome to diet 1"
            print ("called full title")
        }

        // Repeat if statement for storyboards 2 & 3 with different titles.


        // Rest of Class code - Bar Button Item Setup
        self.hidesBackButton = false
        self.leftItemsSupplementBackButton = true

        // Left Side Button Setup

        let buttonView = UIToolbar()
            buttonView.frame = .init(x: 0, y:-7, width: 30, height: 30)

        // Setting UIToolbar Transparency of the toolbar 
            buttonView.setBackgroundImage(UIImage(),
                                        forToolbarPosition: .any,
                                        barMetrics: .default)
            buttonView.setShadowImage(UIImage(), forToolbarPosition: .any)


        // This is a UIView the Toolbar will sit inside which will then be placed into the Navigationbar.
        let navigationContainer = UIView()
        navigationContainer.frame = .init(x: 0, y:0, width: 30, height: 30)

        // ButtonBarItems Set Up

        let ModalButton = UIBarButtonItem(barButtonSystemItem: .compose, target: self, action: #selector(NotesButtonClicked))

        let negativeSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: self, action: nil)
        negativeSpace.width = -10

        //Add ButtonBarItems into the UIToolbar ButtonView
            buttonView.setItems([negativeSpace, ModalButton], animated: false)

        // Add UItoolbar into UIView
            navigationContainer.addSubview(buttonView)

        //Icon Size to fit
            buttonView.sizeToFit()

        //Convert UIView into Barbutton Item to set in the Navigation Bar
        let topLeft = UIBarButtonItem()
        topLeft.customView = navigationContainer

        self.setLeftBarButton(topLeft, animated: false)


        // Setup of the Right Handside Menu Button

        let BurgerButton = UIButton()
            BurgerButton.frame = .init(x: -1, y: 0.5, width: 62, height: 25)
            BurgerButton.setBackgroundImage(UIImage(named: "BurgerButton1.pdf"), for: .normal)


            BurgerButton.addTarget(self, action: #selector(MenuClicked), for: .touchUpInside)

        let topRight = UIBarButtonItem()
        topRight.customView = BurgerButton

        self.setRightBarButton(topRight, animated: true)

    }

        // Calls the Left Button Action. 

        func NotesButtonClicked(_ sender: UIBarButtonItem) {

            print("Notes Modal Is Called")

            //Code here

    }

        // Calls the Right Button Action. 

        func MenuClicked(_ sender: UIButton) {

            print("Menu is Opened - Rightside")

            //Code here

    }
}

好吧,你正在做一些行不通的事情:

    let currentstoryboard = UIStoryboard()

您刚刚创建了一个(空)UIStoryBoard 对象的 NEW 实例...

    let storyboard1 = UIStoryboard(name: "First", bundle: nil)

您刚刚创建了 UIStoryBoard 对象的 NEW 实例,引用名为 "First" 的故事板(假设它存在于您的应用程序中)。

这两个实例永远不会相等。

UIViewController有一个.storyboard属性,也就是"The storyboard from which the view controller originated."

但是,即使您尝试过:

    let currentstoryboard = self.storyboard
    let storyboard1 = UIStoryboard(name: "First", bundle: nil)
    if currentstoryboard == storyboard1 { ... }

它们将仍然永远不相等,因为storyboard1是一个新的实例。

可以尝试:

    let sbName = self.storyboard?.value(forKey: "name") as? String
    if sbName == "First" { ... }

不幸的是,Apple 似乎没有记录,并且很可能会导致您的应用程序被拒绝

我认为您最好使用自己的变量/属性来跟踪内容。