自定义UINavigationItem类 - 依赖于Active UIStoryboard的标题

时间:2017-07-27 10:30:00

标签: ios swift uistoryboard uinavigationitem

我创建了一个有三个故事板的应用程序。 (和多个VC)我正在创建一个全局的NavigationItem类,我可以在故事板中输入,所以每个视图控制器都继承相同的信息,我只需要编写一个页面而不是复制和粘贴到每个VC中。

我想以编程方式设置标题。标题可以根据活动的故事板和手机的大小而改变。即如果它在iPhone SE上显示,则为完整标题或速记。

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

到目前为止,我的NavigationItemCustom类的代码设置如下:我发现很难找到我所在的故事板?请允许任何人向我推进正确的方向。我的代码如下:

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

    }
}

1 个答案:

答案 0 :(得分:0)

嗯,你正在做一些不起作用的事情:

UIStoryBoard

您刚刚创建了一个(空) let storyboard1 = UIStoryboard(name: "First", bundle: nil) 对象的实例...

UIStoryBoard

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

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

.storyboard具有 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" { ... } 新的实例。

可能尝试:

index

不幸的是, 似乎没有被Apple记录,可能会让您的应用被拒绝

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