当用户按下主视图控制器上的按钮时,我正在以编程方式显示模态视图控制器。我的问题是模式视图显示时没有导航栏。
我如何使栏出现?请找到我的代码,让我知道缺失或错误的地方:
-(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];
我应该在主视图控制器的方法中还是在模态视图控制器本身的方法中包括此行?
删除[self presentViewController:iVC动画:是完成:无];仅呈现UINavigationController。
顺便说一句:用“ navBar”命名UINavigationController是不合适的。
-(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];
}
Swift 5
// 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))
// 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()
//Pushing the navigationController to the new viewController
let navController = UINavigationController(rootViewController: addVC)
//Presenting the second screen modally
navigationController?.present(navController, animated: true, completion: nil)
}
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..")
}
}