- UIView
- UIViewController
- UITabBarController
- UINavigationController
- UIWindow
- 常见导航方式
UIView
UIView负责展示,数据控制交给UIViewController。
CGRect frame设置view的左上角位置和大小,改变frame会导致center的改变。
CGPoint center是view的中心点,改变center会导致frame的改变。
(void)addSubview:(UIView *)view;来添加子view,先添加的先压栈,后添加的view显示层级在上面。
@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIView *view = [[UIView alloc] init]; view.backgroundColor = [UIColor redColor]; view.frame = CGRectMake(100, 100, 100, 100); [self.view addSubview:view]; } @end
常用的生命周期:
willMoveToSuperview didMoveToSuperview willMoveToWindow didMoveToWindow
UIViewController
UIViewController自身包含一个default UIView,可以理解为包含多个UIView的容器,用于管理view的生命周期,view的切换,响应用户操作(如滚动,点击)等。常用的生命周期:
init viewDidLoad viewWillAppear viewDidAppear viewWillDisappear viewDidDisappear Dealloc
使用UIViewController可以管理多个UIView进行展示,同样的,UIViewController也可以管理多个UIViewController进行展示,例如UITabBarController
UITabBarController
UITabBarController继承自UIViewController,代表MVC中的C用于控制,通过setViewControllers方法设置viewControllers属性管理切换多个UIViewController。
UITabBar *tabBar属性代表MVC中的V用于展示底部tab。
UITabBarItem *tabBarItem属性代表MVC中的M用于设置数据。
UITabBarController *tabBarC = [[UITabBarController alloc] init]; // 创建 UITabBarController UIViewController *newsVC = [[UIViewController alloc] init]; // 创建 UIViewController newsVC.tabBarItem.title = @"新闻"; newsVC.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/news@2x.png"]; newsVC.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/news_selected@2x.png"]; UIViewController *videoVC = [[UIViewController alloc] init]; videoVC.tabBarItem.title = @"视频"; videoVC.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/video@2x.png"]; videoVC.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/video_selected@2x.png"]; ... // 将 UIViewController 加入 UITabBarController [tabBarC setViewControllers:@[newsVC, videoVC, likeVC, meVC]];
UINavigationController
除了UITabBarController外,还有UINavigationController也是常见的页面切换方式,继承自UIViewController,代表MVC中的C用于控制。通过push/pop来管理UIViewController栈,处理页面间的跳转。
UINavigationBar *navigationBar属性代表MVC中的V用于展示顶部导航条。
UINavigationItem *navigationItem属性代表MVC中的M用于设置数据。
UINavigationController初始化时,需要一个兜底的UIViewController,当栈内全都pop出去后,显示这个兜底的view。
ViewController *newsVC = [[ViewController alloc] init]; UINavigationController *newsNaviC = [[UINavigationController alloc] initWithRootViewController:newsVC];
pushViewController用于push进栈,这样能实现view的跳转,栈顶的UIViewController会被展示出来。
// 使用默认的页头导航条,后退按钮会自动执行pop出栈 [self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES]; // 自定义页头导航条,设置标题和右上按钮 UIViewController *viewVC = [[UIViewController alloc] init]; viewVC.navigationItem.title = @"内容"; viewVC.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"右侧标题" style:UIBarButtonItemStylePlain target:self action:nil]; [self.navigationController pushViewController:viewVC animated:YES];
UIWindow
UIWindow是个特殊形式的UIView,只作为基础容器和各种ViewController一起工作。通常一个APP只要一个UIWindow(但也可以创建多个)。
XCode创建工程时,storyBoard会替我们自动创建一个UIWindow,我们只需要通过rootViewController设置APP的基础ViewController,例如UITabBarController,UINavigationController,并且调用makeKeyAndVisible将其展示出来。
// XCode 创建工程会自动创建一个 UIWindow,你可以删除 Main.storyBoard 文件,手动创建 UIWindow self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; UITabBarController *tabbarC = [[UITabBarController alloc] init]; ... self.window.rootViewController = tabbarC; [self.window makeKeyAndVisible];
常见导航方式
方式一:UIWindow 加载 UITabBarController,每个 tab 加载 UINavigationController,UINavigationController 里加载 UIViewController。这样即使上面页面切换,也不影响底部tabBar,底部tabBar永远存在。
UITabBarController *tabbarC = [[UITabBarController alloc] init]; ViewController *newsVC = [[ViewController alloc] init]; UINavigationController *newsNaviC = [[UINavigationController alloc] initWithRootViewController:newsVC]; newsNaviC.tabBarItem.title = @"新闻"; newsNaviC.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/page@2x.png"]; newsNaviC.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/page_selected@2x.png"]; ... [tabbarC setViewControllers:@[newsNaviC, ...]]; self.window.rootViewController = tabbarC; [self.window makeKeyAndVisible];
方式二(常见):UIWindow 加载 UINavigationController,UINavigationController 加载 UITabBarController,tabBar 里加载 UIViewController。这样上面的页面切换,会同时隐藏底部tabBar。
UITabBarController *tabbarC = [[UITabBarController alloc] init]; ViewController *newsVC = [[ViewController alloc] init]; newsVC.tabBarItem.title = @"新闻"; newsVC.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/page@2x.png"]; newsVC.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/page_selected@2x.png"]; ... [tabbarC setViewControllers:@[newsVC, ...]]; UINavigationController *naviC = [[UINavigationController alloc] initWithRootViewController:tabbarC]; self.window.rootViewController = naviC; [self.window makeKeyAndVisible];