为什么这不打开导航控制器窗口? [英] Why does this not open a navigation controller window?

查看:28
本文介绍了为什么这不打开导航控制器窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下代码在按下相机按钮时没有打开导航控制器?done 函数指定相机应该推送视图控制器,但什么也没有发生.

func setNavigationBar() {让 screenSize: CGRect = UIScreen.main.bounds让 navBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: screenSize.width, height: 160))让 navItem = UINavigationItem(title: "你好")让 doneItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.camera, target: nil, action: #selector(done))navItem.rightBarButtonItem = doneItemnavBar.setItems([navItem], 动画: false)self.view.addSubview(navBar)}@objc func done() {//为 Swift 3 移除 @objc让 dummyViewController = UIViewController()导航控制器? .pushViewController(dummyViewController,动画:真)}

这是 AppDelagate:

@UIApplicationMain类 AppDelegate: UIResponder, UIApplicationDelegate {变量窗口:UIWindow?func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey: Any]?) ->布尔{Thread.sleep(forTimeInterval: 2.0)FirebaseApp.configure()window = UIWindow(frame: UIScreen.main.bounds)window?.makeKeyAndVisible()窗口?.rootViewController =UINavigationController(rootViewController: MessagesController())UITableViewCell.appearance().textLabel?.textColor = UIColor.white;UITableViewCell.appearance().backgroundColor = UIColor.clearUIApplication.shared.statusBarStyle = .lightContentUINavigationBar.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返回真}func applicationWillResignActive(_ application: UIApplication) {让 im = UIImageView()im.tag = 12im.frame = (self.window?.frame)!im.image = UIImage.init(named: "launchbackground.png")application.keyWindow?.subviews.last?.addSubview(im)}func applicationDidEnterBackground(_ application: UIApplication) {让 im = UIImageView()im.tag = 12im.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{对于 sub.subviews 中的 vv{如果(vv.tag == 12){vv.removeFromSuperview()}}}}func applicationDidBecomeActive(_ application: UIApplication) {if let sub = application.keyWindow?.subviews.last{对于 sub.subviews 中的 vv{如果(vv.tag == 12){vv.removeFromSuperview()}}}}func applicationWillTerminate(_ application: UIApplication) {//当应用程序即将终止时调用.如果合适,保存数据.另请参阅 applicationDidEnterBackground:.}}

还有什么我可以添加或删除的东西来帮助解决这个问题,欢迎提出任何建议.

更新:

所以基本上我有一个根视图控制器,它只在用户登录后显示.在用户登录之前,他们会看到我现在正在尝试构建的另一个视图控制器;这个视图控制器是一个注册页面,我需要在顶部有一个导航栏和一个登录"选项计划是让用户点击登录"导航控件并显示导航控制器(随附在右侧)显示登录表单.

解决方案

如果你只是想在导航栏中放置一个相机按钮来推动另一个视图控制器,有一个更简单的方法来做到这一点:

<块引用>

func setNavigationBar() {让 saveButton = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(done))navigationItem.rightBarButtonItem = saveButton}@objc func done() {//为 Swift 3 移除 @objc让 dummyViewController = UIViewController()导航控制器? .pushViewController(dummyViewController,动画:真)}

编辑

所以,如果我理解得很好,你正在展示一个注册页面视图控制器,这就是你需要一个导航按钮来导航到下一个视图的地方,对吗?问题可能是你如何呈现"这个视图控制器.如果您使用方法 present(_:animated:completion:),视图将以模态呈现,这意味着它不会有导航控制器或导航栏.

要解决这个问题,您可以创建一个 UINavigationController,将您的注册页面设置为 rootViewController,并改为呈现导航控制器.

试试这个:

让 registerViewController = RegisterViewController()让 registerNavigationController = UINavigationController(rootViewController: registerViewController)currentViewController.present(注册导航控制器,动画:真)

Why does the following code not open a navigation controller when pressing on the camera button? The function done specifies that the camera should push view controller but nothing happens.

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)
}

This is the 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:.
}




}

Is there anything else I can add or remove to help solve this issue, any suggestions are welcome.

UPDATE:

So basically I have a root view controller that is only shown after a user is logged in. Before the user is logged in they are presented with another view controller that I am trying to build right now; this view controller is a register page that I need to have a navigation bar at the top with an option to "Login" The plan was to have the user to tap on the "Login" navigation control and be presented with navigation controller (that comes in from the right) that displays the login form.

解决方案

If you are just trying to put a camera button in your navigation bar to push another view controller, there's a simpler way to do that:

func setNavigationBar() {

    let saveButton = UIBarButtonItem(barButtonSystemItem: .camera, target: self, action: #selector(done))
    navigationItem.rightBarButtonItem = saveButton
}

@objc func done() { // remove @objc for Swift 3
    let dummyViewController = UIViewController()
    navigationController?.pushViewController(dummyViewController, animated: true)
}

EDIT

So, if I understand well, you are presenting a Register Page view controller, and that’s where you need a navigation button to navigate to the next view, is that right? The problem might how you are ‘presenting’ this view controller. If you are using method present(_:animated:completion:), the view will be presented modally, which means it will not have a navigation controller nor a navigation bar.

To solve this, you can create a UINavigationController, setting your Register Page as the rootViewController, and present the Navigation Controller instead.

Try this:

let registerViewController = RegisterViewController()
let registerNavigationController = UINavigationController(rootViewController: registerViewController)
currentViewController.present(registerNavigationController, animated: true)

这篇关于为什么这不打开导航控制器窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆