【问题标题】:UINavigationController not showing on modal view controllerUINavigationController 未显示在模态视图控制器上
【发布时间】:2013-11-17 14:22:19
【问题描述】:

当用户按下主视图控制器上的按钮时,我以编程方式显示模态视图控制器。我遇到的问题是模态视图显示时没有导航栏。

我怎样才能让栏出现?请找到我的代码,让我知道缺少什么或错误:

-(void)appInfoButtonPressed:(id)sender {
infoViewController *iVC=[[infoViewController alloc] init];
self.modalPresentationStyle = UIModalPresentationCurrentContext;
[self presentViewController:iVC animated:YES completion:nil];
UINavigationController *navBar = [[UINavigationController alloc] initWithRootViewController:iVC];
[self presentViewController:navBar animated:YES completion:nil];

}

另外,在尝试关闭模态视图时,我打算使用以下行:

[self dismissViewControllerAnimated:YES completion:nil];

我应该将此行包含在主视图控制器的方法中还是模式视图控制器本身的方法中?

【问题讨论】:

    标签: ios cocoa-touch uinavigationcontroller modalviewcontroller navigationbar


    【解决方案1】:

    删除 [self presentViewController:iVC animated:YES completion:nil]; 仅用于呈现 UINavigationController。

    顺便说一句:UINavigationController 不适合用“navBar”命名。

    -(void)appInfoButtonPressed:(id)sender {
        infoViewController *iVC=[[infoViewController alloc] init];
        self.modalPresentationStyle = UIModalPresentationCurrentContext;
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:iVC];
        [self presentViewController:navController animated:YES completion:nil];
    }
    

    【讨论】:

    • 非常感谢!固定的!我还找到了问题第二部分的答案。刚刚将此方法添加到模态视图控制器 -(void)dismissModal:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; }
    【解决方案2】:

    斯威夫特 5

     import UIKit
    
    class ListVC: UIViewController {
    
    
        // MARK: - Init
        override func viewDidLoad() {
               super.viewDidLoad()
    
            //Background of the first screen
            view.backgroundColor = .yellow
    
            //Calling the instance of the navigation controller
            let nav = self.navigationController?.navigationBar
    
            //Defining the black theme on the navigation controller
            nav?.barStyle = UIBarStyle.black
    
            //Defining the white characters to make contrast with the black theme on the navigation controller
            nav?.tintColor = UIColor.white
    
            //Defining the custom color of the title font from navigation controller
            nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange]
    
            //Defining the title of the navigation controller
            nav?.topItem?.title = "List"
    
            navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: #imageLiteral(resourceName: "AddBtn"), style: .plain, target: self, action: #selector(hello))
    
    
    
    //        print(Realm.Configuration.defaultConfiguration.fileURL)
    
               let realm = try! Realm()
    
               print(Realm.Configuration.defaultConfiguration.fileURL)
    
           }
    
    
        // MARK: - Selector
    
        /// A selector function that is called when the 'add' button is pressed on the navigation controller
        @objc func hello() {
    
            //Instance of the second screen
            let addVC = AddVC()
    
            //Add the navigationController to the new viewController
            let navController = UINavigationController(rootViewController: addVC)
    
            //Presenting the second screen modally
            navigationController?.present(navController, animated: true, completion: nil)
        }
    
    
    }
    //Other class
    import UIKit
    
    class AddVC: UIViewController {
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            //Background of the view
            view.backgroundColor = .white
    
            //Calling the instance of the navigation controller
            let nav = self.navigationController?.navigationBar
    
            //Initialize the title for the ViewController
            nav?.topItem?.title = "Andrey"
    
            // Initialize the right bar button item
            navigationItem.rightBarButtonItem = setUpSaveButton()
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            self.navigationController?.setNavigationBarHidden(false, animated: true)
        }
    
        /// Function that returns the "Save" bar button item
        private func setUpSaveButton() -> UIBarButtonItem {
            let button = UIBarButtonItem(title: "Save", style: .plain, target: self, action: #selector(saveAction))
            button.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.systemBlue],
                                          for: .normal)
    
            return button
        }
    
        @objc func saveAction() {
            print("Saving..")
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2012-07-24
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多