如何将 NavigationItem 添加到以编程方式创建的 UINavigationController
How to add a NavigationItem to a programmatically created UINavigationController
我正在使用此代码以编程方式将 UINavigationController 添加到现有的 UIViewController:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let nav1 = UINavigationController()
let mainView = ReminderController(nibName: nil, bundle: nil)
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
现在我想向由 UINavigationController 管理的 NavigationBar 添加标题,但大多数情况下我都会收到此错误:
Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller
我使用的代码:
let item = UINavigationItem(title: "Reminder")
nav1.navigationBar.pushNavigationItem(item, animated: false)
我试过的任何其他方法都给了我同样的错误。那么如何给 NavController 添加标题呢?
导航栏现在看起来像这样:
在Objective-C
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:@"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(flipView:)];
nav1.navigationBar.navigationItem.rightBarButtonItem = flipButton;
在Swift
let barBtnVar = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: #selector(Nam1BarBtnKlkFnc(_:)))
self.navigationItem.setRightBarButtonItem(barBtnVar, animated: true)
您的 NavigationBar 由 NavigationController 管理,因此您不能将 Item 直接推送到它。相反,您应该在 mainView 上设置 navigationItem,NavigationController 会负责将其推送到栏
另外,查看 UINavigationController 的 documentation,我认为它可以让您更好地理解 navigationController 的工作原理。
这是来自它:
A navigation controller builds the contents of the navigation bar
dynamically using the navigation item objects (instances of the
UINavigationItem class) associated with the view controllers on the
navigation stack.
在您的 viewDidLoad
方法中试试这个:
let label = UILabel(text: "Reminder")
self.navigationController?.navigationBar.topItem?.titleView = label
我正在使用此代码以编程方式将 UINavigationController 添加到现有的 UIViewController:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let nav1 = UINavigationController()
let mainView = ReminderController(nibName: nil, bundle: nil)
nav1.viewControllers = [mainView]
self.window!.rootViewController = nav1
self.window?.makeKeyAndVisible()
现在我想向由 UINavigationController 管理的 NavigationBar 添加标题,但大多数情况下我都会收到此错误:
Cannot call pushNavigationItem:animated: directly on a UINavigationBar managed by a controller
我使用的代码:
let item = UINavigationItem(title: "Reminder")
nav1.navigationBar.pushNavigationItem(item, animated: false)
我试过的任何其他方法都给了我同样的错误。那么如何给 NavController 添加标题呢?
导航栏现在看起来像这样:
在Objective-C
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:@"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(flipView:)];
nav1.navigationBar.navigationItem.rightBarButtonItem = flipButton;
在Swift
let barBtnVar = UIBarButtonItem(barButtonSystemItem: .Edit, target: self, action: #selector(Nam1BarBtnKlkFnc(_:)))
self.navigationItem.setRightBarButtonItem(barBtnVar, animated: true)
您的 NavigationBar 由 NavigationController 管理,因此您不能将 Item 直接推送到它。相反,您应该在 mainView 上设置 navigationItem,NavigationController 会负责将其推送到栏
另外,查看 UINavigationController 的 documentation,我认为它可以让您更好地理解 navigationController 的工作原理。
这是来自它:
A navigation controller builds the contents of the navigation bar dynamically using the navigation item objects (instances of the UINavigationItem class) associated with the view controllers on the navigation stack.
在您的 viewDidLoad
方法中试试这个:
let label = UILabel(text: "Reminder")
self.navigationController?.navigationBar.topItem?.titleView = label