iOS应用开发实战(10-13)-NavigationController & TabBarController

NavigationController & TabBarController都是界面控制的方式,在使用上感觉很近似,因此就把它们的笔记放在一起了。

Navigation Bar

Navigation Bar:最常用的界面跳转控制方法之一
每个被管理的View Controller要提供:

  • 内容
  • 导航栏标题
  • 导航栏上额外按键
  • 工具栏(可选)按键

图示

navigation_interface

navigation_interface_2x.png

nav_controllers_objects

nav_controllers_objects.jpg

结构:管理的VC,公共界面、Delegate

结构.jpg

Lifecycle

nav_controller_notifications.png
  • Tip:传递数据建议用Segue方法

在代码中使用导航

创建

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController; // Convenience method pushes the root view controller without animation.
- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass;
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated ; // If animated is YES, then simulate a push or pop depending on whether the new top view controller was previously in the stack.

跳转

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated; // Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.
- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated; // Returns the popped controller.
- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated; // Pops until there's only a single view controller left on the stack. Returns the popped controllers.
- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; // Pops view controllers until the one specified is on top. Returns the popped controllers.
- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender ; // Interpreted as pushViewController:animated:

设置和控制

@property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;
@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers; // The current view controller stack.
@property(nonatomic,readonly) UINavigationBar *navigationBar; // The navigation bar managed by the controller. Pushing, popping or setting navigation items on a managed navigation bar is not supported.
@property(null_resettable,nonatomic,readonly) UIToolbar ; // For use when presenting an action sheet.

界面定制

Demo:helloNavUI

helloNavUI.png

UINavigationBar

NavigationBar结构.jpg

UIBarButtonItem,UIBarButtonSystemItem

UIBarButtonItem.png

UITabBarController

一种分页的方法

图示

tabbar_compare.jpg

UITabBarController的结构

结构.png

代码

//创建
UITabBarController *vc =[[UITabBarController alloc]init];
vc.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"HOME" image:@"first" tag:0];

//管理
[self.tabBarController setViewControllers:@"FirstViewController" animated:YES];

//add
[self.view addSubview:vc];

//选中
vc.selectedViewController =vc;
vc.selectedIndex = 1;

界面定制

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容