目录
通过UIView和UIViewController的特性搭建App
UIView
布局
设置大小、位置
addSubView
使用栈管理全部的子View
位置重叠的展示最后入栈(栈顶)的元素
可随时调整位置
插入到指定位置
UIView 生命周期
init -> willMobeToSuperView -> didMoveToSuperView -> willMoveToWindow -> didMoveToWindow
UIViewController
示图管理器,管理视图View层级结构
自身包含View,可以看做一个容器
管理View视图的生命周期
相应用户操作
和App整体交互,视图的切换
作为container管理多个Controller和动画
UIViewController生命周期
init -> loadView -> viewDidLoad -> viewWillAppear -> viewDidAppear -> viewWillDisappear -> viewDidDisapear -> viewDidUnload -> dealloc
通过UIView和UIViewController的特性搭建App
UIView负责页面内的内容呈现
使用基础的UIViewController管理多个UIView
UIViewController在管理UIView的同时,负责不同页面的切换
常用App页面结构分析
单页面展示
通过列表展示简介
通过较长滚动页面展示内容
多页面管理
n个底部按钮
通过Push的方式进行页面的切换
UITabBarController
提供可自定义的底部页面切换按钮
主要功能:管理多个ViewController切换
UITabBarController
ViewControllers TabBar
包括UITabBarButton、tabBarItem.image、tabBarItem.title、UITabBar
按照加入TabBarController的顺序展示
展示的内容有对应的ViewController设置
系统负责点击的响应和切换
iOS13中appdelegate的职责发现了改变:
iOS13之前,Appdelegate的职责全权处理App生命周期和UI生命周期;
iOS13之后,Appdelegate的职责是: 1、处理 App 生命周期 2、新的 Scene Session 生 命周期 那UI的生命周期交给新增的Scene Delegate处理
系统自定义函数实现简易Tabbar:
//实现简易的Tabbar效果
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
UIWindowScene *windowScene = (UIWindowScene *)scene;
self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
self.window.frame = windowScene.coordinateSpace.bounds;
UITabBarController *tabbarController= [[UITabBarController alloc] init];
UIViewController *controller1 = [[UIViewController alloc] init];
controller1.view.backgroundColor = [UIColor whiteColor];
controller1.tabBarItem.title = @"新闻";
controller1.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/page@2x.png"];
controller1.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/page_selected@2x.png"];
UIViewController *controller2 = [[UIViewController alloc] init];
controller2.view.backgroundColor = [UIColor grayColor];
controller2.tabBarItem.title = @"视频";
controller2.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/video@2x.png"];
controller2.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/video_selected@2x.png"];
UIViewController *controller3 = [[UIViewController alloc] init];
controller3.view.backgroundColor = [UIColor lightGrayColor];
controller3.tabBarItem.title = @"推荐";
controller3.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/like@2x.png"];
controller3.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/like_selected@2x.png"];
UIViewController *controller4 = [[UIViewController alloc] init];
controller4.view.backgroundColor = [UIColor orangeColor];
controller4.tabBarItem.title = @"我的";
controller4.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/home@2x.png"];
controller4.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/home_selected@2x.png"];
[tabbarController setViewControllers:@[controller1, controller2, controller3, controller4]];
self.window.rootViewController = tabbarController;
[self.window makeKeyAndVisible];
}
UINavigationController
1.通过栈管理页面间的跳转
2.通常只展示栈顶页面
3.Push/Pop操作
通过UINavigationBar响应操作,处理UIViewController的切换
简易NavigationBar实现
// ViewController.m
#import "ViewController.h"
@interface TestView:UIView
@end
@implementation TestView
-(instancetype)init{
self = [super init];
if(self){
}
return self;
}
- (void)willMoveToSuperview:(nullable UIView *)newSuperview{
[super willRemoveSubview:newSuperview];
}
- (void)didMoveToSuperview;
{
[super didMoveToSuperview];
}
- (void)willMoveToWindow:(nullable UIWindow *)newWindow{
[super willMoveToWindow:newWindow];
}
- (void)didMoveToWindow{
[super didMoveToWindow];
}
@end
@interface ViewController ()
@end
@implementation ViewController
-(instancetype)init{
self = [super init];
if(self){
}
return self;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
TestView *view = [[TestView alloc] init];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:view];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushController)];
[view addGestureRecognizer:tapGesture];
}
-(void)pushController{
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.backgroundColor = [UIColor whiteColor];
viewController.navigationItem.title = @"内容";
viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"右侧标题" style:UIBarButtonItemStylePlain target:self action:nil];
[self.navigationController pushViewController:viewController animated:YES];
}
@end
// SceneDelegate.m
#import "SceneDelegate.h"
#import "AppDelegate.h"
#import "ViewController.h"
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
UIWindowScene *windowScene = (UIWindowScene *)scene;
self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
self.window.frame = windowScene.coordinateSpace.bounds;
UITabBarController *tabbarController= [[UITabBarController alloc] init];
ViewController *viewController = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
// UIViewController *controller1 = [[UIViewController alloc] init];
// controller1.view.backgroundColor = [UIColor whiteColor];
navigationController.tabBarItem.title = @"新闻";
navigationController.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/page@2x.png"];
navigationController.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/page_selected@2x.png"];
UIViewController *controller2 = [[UIViewController alloc] init];
controller2.view.backgroundColor = [UIColor grayColor];
controller2.tabBarItem.title = @"视频";
controller2.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/video@2x.png"];
controller2.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/video_selected@2x.png"];
UIViewController *controller3 = [[UIViewController alloc] init];
controller3.view.backgroundColor = [UIColor lightGrayColor];
controller3.tabBarItem.title = @"推荐";
controller3.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/like@2x.png"];
controller3.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/like_selected@2x.png"];
UIViewController *controller4 = [[UIViewController alloc] init];
controller4.view.backgroundColor = [UIColor orangeColor];
controller4.tabBarItem.title = @"我的";
controller4.tabBarItem.image = [UIImage imageNamed:@"icon.bundle/home@2x.png"];
controller4.tabBarItem.selectedImage = [UIImage imageNamed:@"icon.bundle/home_selected@2x.png"];
[tabbarController setViewControllers:@[navigationController, controller2, controller3, controller4]];
self.window.rootViewController = tabbarController;
[self.window makeKeyAndVisible];
}
- (void)sceneDidDisconnect:(UIScene *)scene {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
- (void)sceneDidBecomeActive:(UIScene *)scene {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
- (void)sceneWillResignActive:(UIScene *)scene {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
- (void)sceneWillEnterForeground:(UIScene *)scene {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
- (void)sceneDidEnterBackground:(UIScene *)scene {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
// Save changes in the application's managed object context when the application transitions to the background.
[(AppDelegate *)UIApplication.sharedApplication.delegate saveContext];
}
@end
1094





