gpt4 book ai didi

ios - 如何从自定义 View Controller 类加载初始 View Controller

转载 作者:行者123 更新时间:2023-11-29 05:49:18 25 4
gpt4 key购买 nike

在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com