码迷,mamicode.com
首页 > 移动开发 > 详细

iOS中UIView之间布局及跳转的几种方式

时间:2015-06-16 21:18:02      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:ios   uiview   跳转   

UIView是iOS开发中所有视图的基类, 表示屏幕上的一块矩形区域, 同时可以处理该区域的绘制和触摸事件. UIViewController是视图控制器的基类, 用来处理屏幕之间的切换等操作, 提供视图管理模型. 一个UIViewController管理一个层级的UIView. 而RootViewController就是iOS应用启动时被载入的第一个视图控制器(可在main.storyboard中指定), 展示APP启动成功后的第一个界面. 因此, iOS中在各个UIViewController之间的切换操作显得尤为重要, 其直接决定了应用各个界面之间的切换效果. 主要的跳转方式有以下几种:

UITabBarController

UITabBarController主要用于平级View的跳转, 应用案例如微信界面下方的四个Tab。可选中一个UIViewController, 在Xcode->Editor->Embed In-> Tab Bar Controller将其加入到一个Tab bar中. 也可以在storyboard中直接从UITabBarController中连线至一个View, 选择Relationship Segue的view controllers即可.

property

tabbar里边包含的每一个viewController都对应一个tabbarItem, 位置都是均分的, 最多显示4个tab, 再多了就会折叠起来. 通过代码来设置tabbar的时候, 可以使用setViewControllers来添加指定的子ViewController为其item.
tabbarItem有title, image, selectedImage, badgeValue属性, badgeValue是该item右上角的提醒数字. 而selectedIndex和selectedViewController则定位到当前选取的tabbarItem.
除此之外, 还有viewControllers, selectedViewController, selectedIndex等属性, 含义就不罗嗦了.

UITabBarControllerDelegate

该协议用于在选取某一个tabbarItem的时候, 执行一些额外的操作, 监控tabbar的改变, 也可以阻止某一个tabbarItem被选取.

UINavigationController

堆栈式View管理

UINavigationController是IOS开发中常用的用于视图切换的控制器, 提供堆栈式的View管理方式, RootViewController在stack的最底层. 提供了诸多方法用于进行view之间的切换及管理等, 如
pushViewController与popViewController等. 详细内容, 可参考之前的一篇博客UINavigationController的简单总结.
一般使用UINavigationController的方式, 会自动为我们设置好每个View界面的标题, 左上角的返回按钮, 以及屏幕右滑回退的操作. 如果想要禁止屏幕右滑返回等的手势操作, 可以在当前View的viewDidAppear方法中设置如下:

self.navigationController.interactivePopGestureRecognizer.enabled = NO; // 禁止右滑等手势

需要注意的是 UINavigationController是采用类似stack的push和pop的方式完成view的切换, 调用方法为pushViewController和popViewController. 而segue属性也要相应地设置为push.
使用viewControllers属性可以获取当前的视图栈.

property

toolbarHidden是用于隐藏navigationController最上方的导航工具栏. 在该工具栏中, 我们可以自行添加各种Bar Button Item控件. 常见的是leftBarButtonItem和rightBarButtonItem.
UINavigationItem是该View栈中的每一项. 可以在Storyboard或xib文件中指定, 也可以自行代码创建, 然后加到UINavigationController中去即可.

UINavigationControllerDelegate

该协议为NavigationController中的View跳转, 提供了很多遍历的方法.如: didShowViewController, willShowViewController, animationControllerForOperation等. 而枚举UINavigationControllerOperation中定义了View之间跳转的方式(None, Push, Pop).

使用nib文件

nib文件是一系列UIView的组合.

NSArray *arrayMessage = [[NSBundle mainBundle] loadNibNamed:@“ViewMessageCenter” owner:nil options:nil];
self.vMessageCenter = [arrayMessage objectAtIndex:0];
self.vMessageCenter.navigationController = self.navigationController;
self.vMessageCenter.frame = self.vMainPanel.bounds;

使用storyboard

将一个ViewController放在storyboard中, 然后调用instantiateViewControllerWithIdentifier, 加载一个storyboard文件中的对应ID的storyboard(一系列view的集合), 也是非常常用的一种方式.

UIStoryboard *sb = [UIStoryboard storyboardWithName:@“Main” bundle:nil];
LoginViewController *vc = [sb instantiateViewControllerWithIdentifier:@“LoginViewController”];
[self.navigationController popToRootViewControllerAnimated: YES];
[self.navigationController presentViewController: vc animated: YES completion:nil];

使用nib文件

Nib文件是一种特殊类型的资源文件, 保存Interface Builder文档, 可以进行可视化编辑.
每一个xib文件对应一个ViewController或者一个自定义的View, 可以使用loadNibNamed:方法来加载nib文件

NSArray *arrayMessage = [[NSBundle mainBundle] loadNibNamed:@“ViewMessageCenter” owner:nil options:nil];
self.vMessageCenter = [arrayMessage objectAtIndex:0];
self.vMessageCenter.navigationController = self.navigationController;
self.vMessageCenter.frame = self.vMainPanel.bounds;

segue

对于两个单独的ViewController, 可以使用segue指定跳转方式.
如在storyboard中, 在VC1中的button上右键, 连线至第二个VC, 选择跳转方式即可实现两个VC之间的相互跳转.
如果想通过点击一个image, 实现VC的跳转呢? 这就要引入gesture了.

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

View加载的时候设置手势:

self.imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *imageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped)];
[self.imageView addGestureRecognizer:imageTap];

通过addSubView

如self.view.addSubView(newView) 即可直接加载UIView, 使用removeFromSuperview将该UIView移除.

iOS中UIView之间布局及跳转的几种方式

标签:ios   uiview   跳转   

原文地址:http://blog.csdn.net/icetime17/article/details/45865747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!