UINavigationController是iOS开发中常用的基础控制器之一。它是一种容器控制器,可以用于管理页面的导航栈,并提供页面之间的切换、协调、和管理功能,让用户可以更加方便的进行页面的跳转操作。
简单来说,UINavigationController的作用就是协调导航控制器内的各个视图控制器,以一种层级的方式进行视图展示和切换,为用户提供便利的操作。在应用中,我们可以通过UINavigationController来实现页面间的无缝切换,提升用户体验。
使用UINavigationController的方法:
1. 创建UINavigationController对象
在创建UINavigationController对象时,我们需要把需要展示的UIViewController对象添加到导航栈中。如下代码所示:
```
UIViewController *firstVC = [[UIViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:firstVC];
[self presentViewController:navController animated:YES completion:nil];
```
其中,initWithRootViewController:方法用于设置导航栈中的第一个视图控制器,即根视图控制器。
2. 添加子视图控制器
可以使用pushViewController:animated:方法向导航栈中添加新的视图控制器,如下所示:
```
UIViewController *secondVC = [[UIViewController alloc] init];
[navController pushViewController:secondVC animated:YES];
```
其中,animated参数用于控制视图控制器的转场动画。
3. 返回上一级视图控制器
可以使用popViewControllerAnimated:方法返回到上一个视图控制器,如下所示:
```
[navController popViewControllerAnimated:YES];
```
也可以使用popToViewController:animated:方法返回到指定的视图控制器:
```
[navController popToViewController:firstVC animated:YES];
```
4. 设置导航栏
可以通过UINavigationBar和UIToolbar来定制导航栏和工具栏的样式和内容。例如,可以设置导航栏的背景色、标题、左右按钮等,如下所示:
```
[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:17]}];
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg"] forBarMetrics:UIBarMetricsDefault];
```
5. 自定义转场动画
可以通过UIViewControllerTransitioningDelegate和UIViewControllerAnimatedTransitioning协议来自定义转场动画。具体实现可以参考下面的示例代码:
```
//第一个视图控制器
@interface FirstViewController () @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; self.title = @"First"; self.navigationController.delegate = self; } /** 实现UINavigationControllerDelegate代理 为当前控制器添加转场动画 */ - (nullable id animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { TransitionAnimator *animator = [[TransitionAnimator alloc] init]; animator.animationType = operation; return animator; } /** 跳转到第二个控制器 */ - (void)goToSecondVC { SecondViewController *secondVC = [[SecondViewController alloc] init]; [self.navigationController pushViewController:secondVC animated:YES]; } @end //第二个视图控制器 @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blueColor]; self.title = @"Second"; } @end //自定义转场动画 @interface TransitionAnimator : NSObject @property (nonatomic, assign) UINavigationControllerOperation animationType; @end @implementation TransitionAnimator - (NSTimeInterval)transitionDuration:(nullable id return 0.5; } - (void)animateTransition:(id //获取需要展示的视图控制器,即目标控制器(toVC)和前一个控制器(fromVC) UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIView * containerView = transitionContext.containerView; NSTimeInterval duration = [self transitionDuration:transitionContext]; CGRect fromVCRect = [transitionContext initialFrameForViewController:fromVC]; CGRect toVCRect = [transitionContext finalFrameForViewController:toVC]; //判断是Push还是Pop操作,执行不同的操作动画 if (self.animationType == UINavigationControllerOperationPush) { toVC.view.frame = CGRectOffset(toVCRect, CGRectGetWidth(toVCRect), 0); [containerView addSubview:toVC.view]; [UIView animateWithDuration:duration animations:^{ fromVC.view.frame = CGRectOffset(fromVCRect, -CGRectGetWidth(fromVCRect), 0); toVC.view.frame = toVCRect; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; } else { [containerView insertSubview:toVC.view belowSubview:fromVC.view]; [UIView animateWithDuration:duration animations:^{ fromVC.view.frame = CGRectOffset(fromVCRect, CGRectGetWidth(fromVCRect), 0); toVC.view.frame = toVCRect; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; } } @end ``` 以上是UINavigationController的基础知识和使用方法,接下来我们来看看UINavigationController在实际开发中的应用案例。 案例1: 一个简单的导航栏应用,有两个视图控制器,通过按钮来实现跳转。 ``` //第一个视图控制器 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor yellowColor]; self.title = @"First"; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)]; [button setTitle:@"Go" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goToSecondVC) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } /** 跳转到第二个控制器 */ - (void)goToSecondVC { SecondViewController *secondVC = [[SecondViewController alloc] init]; [self.navigationController pushViewController:secondVC animated:YES]; } //第二个视图控制器 - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blueColor]; self.title = @"Second"; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 50)]; [button setTitle:@"Back" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goToFirstVC) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } /** 返回到第一个控制器 */ - (void)goToFirstVC { [self.navigationController popViewControllerAnimated:YES]; } ``` 案例2: 一个简单的图片浏览应用,通过滑动来实现图片间的切换。 ``` //第一个视图控制器 @interface ViewController () @property (nonatomic, strong) UIScrollView *scrollView; //滚动视图 @property (nonatomic, strong) NSArray *imageArray; //存储图片的数组 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; self.title = @"Photos"; //初始化图片数组 self.imageArray = @[@"image1",@"image2",@"image3",@"image4",@"image5",@"image6"]; //初始化UIScrollView self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds]; self.scrollView.delegate = self; self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(self.view.frame) * self.imageArray.count, CGRectGetHeight(self.view.frame)); self.scrollView.pagingEnabled = YES; [self.view addSubview:self.scrollView]; //添加UIImageView for (int i = 0; i < self.imageArray.count; i++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height)]; imageView.backgroundColor = [UIColor clearColor]; imageView.contentMode = UIViewContentModeScaleAspectFit; imageView.image = [UIImage imageNamed:self.imageArray[i]]; [self.scrollView addSubview:imageView]; } } #pragma mark - UIScrollViewDelegate - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSUInteger index = scrollView.contentOffset.x / CGRectGetWidth(self.view.frame); self.title = [NSString stringWithFormat:@"Photos %d/%d",index + 1, (int)self.imageArray.count]; } @end ``` 以上是UINavigationController的详细介绍和使用方法,通过UINavigationController的使用,我们可以轻松实现应用中的页面导航和转场动画。如果你有更好的实践和体会,欢迎留言分享。 如果你喜欢我们三七知识分享网站的文章,
欢迎您分享或收藏知识分享网站文章
欢迎您到我们的网站逛逛喔!https://www.37seo.cn/
发表评论 取消回复