UINavigationController的 initWithNavigationBarClass:toolbarClass: 创建方法使用

白淇
2023-12-01

只是看到iOS文档中关于此方法的说明,还未经过实际验证,先记录下。

关于该方法的说明如下:

Discussion
Use this initialization method if you want to use custom navigation bar or toolbar subclasses with the navigation controller. If you use this method, you are responsible for adding a root view controller before presenting the navigation controller onscreen.


在网上查询得到答复如下:


// This code assumes `MyCustomNavigationBar` is the name of your custom subclass, and that `viewController` is a UIViewController object created earlier.
// To create the containing navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithNavigationBarClass:[MyCustomNavigationBar class] toolbarClass:[UIToolbar class]];

// To set the root view controller in the navigation controller
navigationController.viewControllers = @[viewController]


The code above informs UIKit to create a UINavigationController with navigation bars of subclass MyCustomNavigationBar.Then, it sets the root view controller to the object stored in the variable viewController.


在UINavigationController的viewControllers属性有如下说明:

The root view controller is at index 0 in the array, the back view controller is at index n-2, and the top controller is at index n-1, where n is the number of items in the array.

即说明在该数组中第一个添加的视图控制器即为根视图控制器(导航控制器的)。

 类似资料: