【iOS】界面推出的方法
在学习过程中我们发现在iOS中有两种界面推出的方法:push 和 present
这两种方法都可以用来推出一个新的界面
但是这两者是存在区别的
push
方法是通过UINavigationController
进行导航,新的视图控制器会被压入导航栈中,可以跨级返回,push
是由UINavigationController管理的视图控制器堆栈,在window下同时只能显示一个ViewController。 push一般用于同一业务不同界面间的切换。present
方法是通过模态展示的方式推出新的视图控制器,present
是由UIViewController管理的视图控制器堆栈,在window下可以以叠加的方式展示,当顶层的view透明时可以看到底层的view,但只有顶层的view可用户交互,而且只能逐级返回。present一般用于不同业务界面的切换
这里要注意的问题主要是push
和pop
结合使用,下面给出一下一些常用的方式。
push 和 pop结合使用
self.window.frame = [UIScreen mainScreen].bounds;
VCRoot* vc = [[VCRoot alloc] init];
//创建导航控制器,用于管理多个视图控制器的切换
//采用层级的方式来管理多个视图的一个切换
//创建的时候一定要有一个根视图控制器
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:vc];//设置一个根视图
//弹回根视图
[self.navigationController popToRootViewControllerAnimated:YES];
VCThird* vc = [[VCThird alloc] init];
//将一个视图控制器推入导航控制器管控的视图控制器堆栈
[self.navigationController pushViewController:vc animated:YES];
[self.navigationController popViewControllerAnimated: YES];//返回上一级
上面是我们的基本上需要使用的函数,下面我们来应用一下这个部分的内容:
在使用之前我们先要设置我们的一个导航控制器的根视图:
#import "VCRoot.h"
#import "VCSecond.h"
@interface VCRoot ()
@end
@implementation VCRoot
- (void)viewDidLoad {
[super viewDidLoad];
//设置透明度,yes透明,no不透明
self.navigationController.navigationBar.translucent = YES;
self.title = @"title";
self.navigationItem.title = @"根视图";
self.view.backgroundColor = [UIColor blueColor];
//设置导航栏的风格默认为Default
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
UIBarButtonItem* next = [[UIBarButtonItem alloc] initWithTitle:@"下一级别" style:UIBarButtonItemStylePlain target:self action:@selector(pressRight)];
self.navigationItem.rightBarButtonItem = next;
// Do any additional setup after loading the view.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (void) pressleft {
NSLog(@"left");
}
- (void) pressRight {
VCSecond* vcSecond = [[VCSecond alloc] init];
//使用这个视图控制器
[self.navigationController pushViewController:vcSecond animated:YES];
}
@end
然后设置这个视图控制器为根视图控制器:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
self.window.frame = [UIScreen mainScreen].bounds;
VCRoot* root = [[VCRoot alloc] init];
UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:root];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
// 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).
}
我们在设置两个视图控制器:
#import "VCSecond.h"
#import "VCThird.h"
@interface VCSecond ()
@end
@implementation VCSecond
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
UIBarButtonItem* btnNext = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(press)];
self.navigationItem.rightBarButtonItem = btnNext;
// Do any additional setup after loading the view.
}
- (void)press {
VCThird* vc = [[VCThird alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
#import "VCThird.h"
@interface VCThird ()
@end
@implementation VCThird
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIBarButtonItem* btnleft = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(press)];
self.navigationItem.rightBarButtonItem = btnleft;
// Do any additional setup after loading the view.
}
-(void) press {
//弹出上一级界面
[self.navigationController popToRootViewControllerAnimated:YES];
}
这样就可以实现一个根视图控制器推出其他视图的效果。
实现效果:
present 和 dismiss结合使用
我们这里基本上是通过这种方式来推出视图控制器和消失视图控制器。
[self presentViewController:vc animated:YES completion: nil];//这里推出我们的view页面。
[self dismissViewControllerAnimated:YES completion: nil];//消失这个view
下面来使用一下这段代码来呈现一下我们的视图:
- (void)creatUIRectButton { //创建普通的UIButton
UIButton *btn = [UIButton buttonWithType: UIButtonTypeRoundedRect];//根据类型来创建
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(10, 400, 120, 80);
[btn1 setTitle:@"test button" forState:UIControlStateNormal];
[btn1 setTitle:@"按下时候" forState:UIControlStateHighlighted];
[btn1 addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];//手指离开时在按钮范围内
[btn1 setTintColor:[UIColor whiteColor]];
btn1.backgroundColor = [UIColor grayColor];
btn.frame = CGRectMake(10, 100, 150, 80);//一个矩型设置的一个位置
[btn setTitle:@"button1" forState: UIControlStateNormal]; //一个是按钮的位置,一个是按钮文字的现显示的状态
[btn setTitle:@"fitst button1" forState: UIControlStateHighlighted];
btn.backgroundColor = [UIColor grayColor];
[btn setTitleColor:[UIColor redColor] forState: UIControlStateNormal];
[btn setTitleColor:[UIColor greenColor] forState: UIControlStateHighlighted];
[btn setTintColor: [UIColor whiteColor]];
btn.titleLabel.font = [UIFont systemFontOfSize:18];
[btn addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 101;
btn1.tag = 102;
[self.view addSubview: btn1];
[self.view addSubview: btn]; //添加到视图中显示
}
- (void) press:(UIButton*) btn {
if (btn.tag == 102) {
View02* vc = [[View02 alloc] init];
[self presentViewController:vc animated:YES completion: nil];
}
if (btn.tag == 101) {
NSLog(@"btn1被按下");
}
}
这部分代码是通过设置一个按钮来实现一个推出按钮的效果。
这时候我们设置另一个视图控制器的内容,设置他只要点击一下就回退到上一层的内容。
#import "View02.h"
@interface View02 ()
@end
@implementation View02
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
// Do any additional setup after loading the view.
}
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//使当前的控制器消失掉
//P1:新的视图对象
//P2:使用动画切换效果
//P3:切换结束后功能调用,不需要就传入一个nil。
[self dismissViewControllerAnimated:YES completion: nil];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
这是一个实现效果: