【发布时间】:2026-02-13 11:55:01
【问题描述】:
为什么下面的代码在按下相机按钮时没有打开导航控制器? done 函数指定相机应该推送视图控制器但没有任何反应。
func setNavigationBar() {
let screenSize: CGRect = UIScreen.main.bounds
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: screenSize.width, height: 160))
let navItem = UINavigationItem(title: "Hello")
let doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.camera, target: nil, action: #selector(done))
navItem.rightBarButtonItem = doneItem
navBar.setItems([navItem], animated: false)
self.view.addSubview(navBar)
}
@objc func done() { // remove @objc for Swift 3
let dummyViewController = UIViewController()
navigationController?.pushViewController(dummyViewController, animated: true)
}
这是 AppDelagate:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
[UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Thread.sleep(forTimeInterval: 2.0)
FirebaseApp.configure()
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController =
UINavigationController(rootViewController: MessagesController())
UITableViewCell.appearance().textLabel?.textColor = UIColor.white;
UITableViewCell.appearance().backgroundColor = UIColor.clear
UIApplication.shared.statusBarStyle = .lightContent
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
UINavigationBar.appearance().barTintColor = UIColor(red:0.13, green:0.15, blue:0.18, alpha:1.0)
UIApplication.shared.isStatusBarHidden = false
return true
}
func applicationWillResignActive(_ application: UIApplication) {
let im = UIImageView()
im.tag = 12
im.frame = (self.window?.frame)!
im.image = UIImage.init(named: "launchbackground.png")
application.keyWindow?.subviews.last?.addSubview(im)
}
func applicationDidEnterBackground(_ application: UIApplication) {
let im = UIImageView()
im.tag = 12
im.frame = (self.window?.frame)!
im.image = UIImage.init(named: "launchbackground.png")
application.keyWindow?.subviews.last?.addSubview(im)
}
func applicationWillEnterForeground(_ application: UIApplication) {
if let sub = application.keyWindow?.subviews.last
{
for vv in sub.subviews
{
if(vv.tag == 12)
{
vv.removeFromSuperview()
}
}
}
}
func applicationDidBecomeActive(_ application: UIApplication) {
if let sub = application.keyWindow?.subviews.last
{
for vv in sub.subviews
{
if(vv.tag == 12)
{
vv.removeFromSuperview()
}
}
}
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
还有什么我可以添加或删除来帮助解决这个问题的,欢迎提出任何建议。
更新:
所以基本上我有一个根视图控制器,它只在用户登录后显示。在用户登录之前,他们会看到另一个我正在尝试构建的视图控制器;这个视图控制器是一个注册页面,我需要在顶部有一个导航栏,并带有“登录”选项。计划是让用户点击“登录”导航控件并显示导航控制器(即in) 显示登录表单。
【问题讨论】:
-
这个视图控制器在导航控制器中吗?
标签: swift uiviewcontroller uinavigationcontroller