【发布时间】:2017-07-27 10:29:59
【问题描述】:
我创建了一个包含三个故事板的应用程序。 (和多个 VC) 我正在创建一个全局 NavigationItem 类,我可以在情节提要中输入它,因此每个视图控制器都继承相同的信息,我只需编写一个页面而不是复制和粘贴到每个 VC 中。
我想以编程方式设置标题。标题可以根据激活的情节提要和手机的尺寸而改变。即在 iPhone SE 上显示的完整标题或简写。
我不能使用。 UIStoryboard 作为布尔值,因为它不符合要求。我怎样才能知道我在哪个故事板中,所以它可以在 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
}
}
【问题讨论】:
标签: ios swift uistoryboard uinavigationitem