Search code examples
iosswiftuistoryboarduinavigationitem

Custom UINavigationItem class - Title dependent on Active UIStoryboard


I've created an App which has three storyboards. ( & multiple VC's) I am creating a global NavigationItem class, that I can enter in storyboard so every view controller inherits the same information and I only have to code one page rather than copying and pasting into each VC.

I want to set the title programmatically. The title can change depending on which storyboard is active and what size phone is present. i.e full title or shorthand if it is displaying on an iPhone SE.

I can't use. UIStoryboard as a bool, as it doesn't conform. How can I tell which storyboard I'm in, so it can set the title in an if statement? The code i've written is wrong and doesn't work for the storyboard.

My code setup so far for my NavigationItemCustom class is bellow I am finding it difficult to find which storyboard i'm in? Can anyone push me in the right direction, please. My code is as follows:

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

    }
}

Solution

  • Well, you are doing a couple things that won't work:

        let currentstoryboard = UIStoryboard()
    

    You just created a NEW instance of a (empty) UIStoryBoard object...

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

    You just created a NEW instance of a UIStoryBoard object, referencing the Storyboard named "First" (assuming it exists in your app).

    Those two instances could never be equal.

    UIViewController has a .storyboard property, which is "The storyboard from which the view controller originated."

    However, even if you tried:

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

    They will still never be equal, because storyboard1 is a new instance.

    You might try:

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

    Unfortunately, that does not seem to be documented by Apple, and may very possibly and up getting your app rejected.

    I'd think you would be much better off using your own variables / properties to keep track of what's what.