作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的 Storyboard
中,我有一个FirstViewController
,它也是一个初始 View Controller
。我的 StoryBoard
中有另一个 UIViewController
,名为 BaseViewController
。我在上面添加了一个 NavigationBar
。我想从 BaseViewController
派生 FirstViewController
,因此我编写了以下代码。
class FirstViewController: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
但是当我运行应用程序时,它没有显示BaseViewController
的内容。相反,它仅加载 FirstViewController 的内容。我希望从 BaseViewController
派生的每个 UIViewControllere
都应该有它的 NavigationBar
。我需要什么改变以获得这个?
最佳答案
如果您的 BaseViewController 的目的只是为 ViewController 提供导航栏。您可以将 FirstViewController(NavController 的根 Controller )嵌入到 NavigationController(设置为初始 View Controller )中。如果您想要不同的 Controller 使用不同的导航栏,您必须像我一样以编程方式设置它。
看这里-
extension UIViewController {
func setNavBar(title : String, modType : ModuleType = .home, barHexColor : String? = nil, isBackBtn : Bool, isMenuBtn : Bool, isJummperBtn : Bool, isScreenshotBtn : Bool) {
k_ModuleType = modType
var barColor = "E14145"
var arrLeftItems = [UIBarButtonItem]()
var arrRightItems = [UIBarButtonItem]()
if let aBarColor = barHexColor {
barColor = aBarColor
}
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.barTintColor = UIColor(hexString: barColor)
self.navigationController?.navigationBar.tintColor = UIColor.white
if isBackBtn {
let btnBack = UIButton(type: .custom)
btnBack.setImage(UIImage(named: "Back2"), for: .normal)
btnBack.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnBack.addTarget(self, action: #selector(self.barBackAction(_:)), for: .touchUpInside)
let itemBack = UIBarButtonItem(customView: btnBack)
arrLeftItems.append(itemBack)
}
if isMenuBtn {
let btnMenu = UIButton(type: .custom)
btnMenu.setImage(UIImage(named: "menu2"), for: .normal)
btnMenu.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnMenu.addTarget(self, action: #selector(self.barMenuAction(_:)), for: .touchUpInside)
let itemMenu = UIBarButtonItem(customView: btnMenu)
arrLeftItems.append(itemMenu)
}
if isScreenshotBtn {
let btnScreenshot = UIButton(type: .custom)
btnScreenshot.setImage(UIImage(named: "screenshot2"), for: .normal)
btnScreenshot.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnScreenshot.addTarget(self, action: #selector(self.barScreenshotAction(_:)), for: .touchUpInside)
let itemScreenshot = UIBarButtonItem(customView: btnScreenshot)
arrRightItems.append(itemScreenshot)
}
if isJummperBtn {
let btnJumpper = UIButton(type: .custom)
btnJumpper.setImage(UIImage(named: "jumpper2"), for: .normal)
btnJumpper.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnJumpper.addTarget(self, action: #selector(self.barJumpperAction(_:)), for: .touchUpInside)
let itemJumpper = UIBarButtonItem(customView: btnJumpper)
arrRightItems.append(itemJumpper)
}
self.navigationItem.setLeftBarButtonItems(arrLeftItems, animated: true)
self.navigationItem.setRightBarButtonItems(arrRightItems, animated: true)
let lblNavTitle = UILabel(frame: CGRect(x: 0, y: 0,
width: DeviceType.isIPad() ? 200 : 110,
height: 40))
lblNavTitle.textAlignment = .center
lblNavTitle.numberOfLines = 0
lblNavTitle.textColor = UIColor.white
lblNavTitle.text = title
lblNavTitle.font = UIFont.systemFont(ofSize: DeviceType.isIPad() ? 21 : 16,
weight: .medium)
lblNavTitle.backgroundColor = UIColor.clear
self.navigationItem.titleView = lblNavTitle
self.navigationItem.titleView?.isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.tapTitle(_:)))
tapGesture.numberOfTapsRequired = 1
self.navigationItem.titleView?.addGestureRecognizer(tapGesture)
}
然后在FirstViewController的Viewdidload()方法中
override func viewdidload()
{
super.viewdidload()
self.setNavBar(title : aTitle,
barHexColor : nil,
isBackBtn : false,
isMenuBtn : true,
isJummperBtn : true,
isScreenshotBtn : true)
}
关于ios - 如何从自定义 View Controller 类加载初始 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55845717/
我是一名优秀的程序员,十分优秀!