在Iphone的视图中,其实就是一个一个view,一层view上面放一层view,一个view上面放一群view,甚至UIWindow也是一个view,在网上找了一张图片很能说明这个问题:

可见我们能够看到的都是一个view视图,而我们能对其进行操作,是因为UIController和UIView都是UIResponder的子类。这时我们对视图进行操作时需要掌握几个比较重要的概念和几个常用的方法.一个是superView和subView的概念,一个是UIView和UIControl类对象的区别。

当我们生成一个独立的view视图时,往往是新建一个UIViewController的文件并伴随一个xib文件,这个xib文件中的fie's owner肯定是这个UIViewController类了,而它的Objects一般就是一个UIView对象(一张干净的画布),我们往往可以将这个UIView改为UIControl,通过修改它的Custom Class项来实现,这时这一大张画布就可以像它上面那些button之流的控件一样来响应方法了,我们在隐藏软键盘时就用到了这一点。

而superView和subView的概念更好理解,view上可以放控件,那么这个view就是这些控件的superView.而UIWindow上可以叠一层又一层的view,UIWindow就是这些view的superView.先放上去的index为0,然后依次累加。

处理superView和subView往往用到几个方法:

removeFromSuperview;//调用者为subView

insertSubview:atIndex;//调用者为superView

了解了这些我们来看一个实例,很简单,实现在根视图控制器上添加两个view(都是整页覆盖屏幕的),然后点击屏幕分别切换显示。

新建一个项目,然后添加两个UIViewController类并附带xib文件,分别取名为FirstViewController,SecondViewController;在viewController中进行如下操作:

viewController.h:

[plain] view plaincopy
  1. #import <UIKit/UIKit.h>
  2. #import "FirstViewController.h"
  3. #import "SecondViewController.h"
  4. @interface ViewController : UIViewController
  5. @property (retain ,nonatomic) FirstViewController *firstViewController;
  6. @property (retain ,nonatomic) SecondViewController *secondViewController;
  7. @end

viewController.m:

[plain] view plaincopy
  1. #import "ViewController.h"
  2. @implementation ViewController
  3. @synthesize firstViewController;
  4. @synthesize secondViewController;
  5. - (void)viewDidLoad
  6. {
  7. [super viewDidLoad];
  8. firstViewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
  9. secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
  10. [self.view addSubview:firstViewController.view];
  11. [self.view addSubview:secondViewController.view];
  12. }
  13. - (void)viewDidUnload
  14. {
  15. [super viewDidUnload];
  16. // Release any retained subviews of the main view.
  17. }
  18. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  19. {
  20. return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  21. }
  22. @end

以上代码实现了在viewDidLoad中声明两个对象,然后添加到主视图上,这样在打开模拟器时会看到secondView的视图(后添加的在上面,index为1);

然后在FirstViewController.xib文件中将View的Custom Class发为UIControl,背景改一下,贴个label内容为1;

在FirstViewController.h中添加一个输出口:

[plain] view plaincopy
  1. #import <UIKit/UIKit.h>
  2. @interface FirstViewController : UIViewController
  3. -(IBAction)clickH:(id)sender;
  4. @end

在FirstViewController.m中进行如下操作:

[plain] view plaincopy
  1. #import "FirstViewController.h"
  2. @interface FirstViewController ()
  3. @end
  4. @implementation FirstViewController
  5. -(IBAction)clickH:(id)sender
  6. {
  7. [self.view.superview insertSubview:self.view atIndex:0];
  8. //将当前的view放到最底部。
  9. }
  10. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  11. {
  12. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  13. if (self) {
  14. // Custom initialization
  15. }
  16. return self;
  17. }
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. // Do any additional setup after loading the view from its nib.
  22. }
  23. - (void)viewDidUnload
  24. {
  25. [super viewDidUnload];
  26. // Release any retained subviews of the main view.
  27. // e.g. self.myOutlet = nil;
  28. }
  29. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  30. {
  31. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  32. }
  33. @end

这样就利用insert方法在点击该视图时将其放入底部,另一个视图就显示出来了,在secondViewController中进行同样的操作,就会实现初始化后点击屏幕来回切换的效果。当然,这是在两个视图的情况下,只是两个不停的换位置的效果,demo用于理解方法,如果是多视图大应用的话常常要进行remove操作,这样会提高效率。

关键字:IOS ,Iphone 开发 ,基础 ,入门 ,superView ,subView ,insertSubview ,removeFromSuperview ,手动多视图切换

IOS superView和subView相关推荐

  1. iOS事件处理,看我就够了~

    该文章属于<简书 - 刘小壮>原创,转载请注明: <简书 - 刘小壮> https://www.jianshu.com/p/b0884faae603 好久没写博客了,前后算起来 ...

  2. iOS事件处理,看我就够了~ 1

    2019独角兽企业重金招聘Python工程师标准>>> 该文章属于<简书 - 刘小壮>原创,转载请注明: <简书 - 刘小壮> https://www.jia ...

  3. 苹果iOS开发系列--详解Swift 3.0语言的重大变化

    概述 从写第一篇Swift文章的时候到现在Swift已经从1.2发展到了今天的3.0,这期间由于Swift目前还在发展阶段并不能向下兼容,因此第一篇文章中的部分代码在当前的Xcode环境中已经无法运行 ...

  4. 48.iOS动画和理解position与anchorPoint

    1.动画的基本概念 动画的使⽤场景:iOS中的动画是指一些视图上的过渡效果,合理利用动画能提⾼用户体验,UIView动画影响的属性 frame:视图框架 center:视图位置 alpha:视图透明度 ...

  5. iOS的layer的anchorpoint与posion问题

    每一个UIView内部都默认关联着一个CALayer, UIView有frame.bounds和center三个属性,CALayer也有类似的属性,分别为frame.bounds.position.a ...

  6. ios 纯代码怎么适配ipad_iPad横竖屏下的代码适配

    你可能非常了解用不同的方式去适配不同尺寸的iPhone屏幕,在适配iPhone屏幕时你需要考虑的只是屏幕大小变化带来的UI元素间隔的变化,但是在iPad上主要针对的是横竖屏下完全不同的UI元素的布局, ...

  7. AutoLayout 浅析动画

    1.AutoLayout相关的几个易混淆的方法 setNeedsLayout layoutIfNeeded layoutSubViews setNeedsUpdateConstraints updat ...

  8. 深入理解Auto Layout 第一弹

    form:https://zhangbuhuai.com/auto-layout-part-1/ By 张不坏 2015-07-16 更新日期:2015-07-17 文章目录 1. 写在前面 2. i ...

  9. 彻底理解position与anchorPoint - Wonderffee's Blog(转)

    引言 相信初接触到CALayer的人都会遇到以下几个问题:  为什么修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与posi ...

  10. 实现炫酷的卡片式动画!

    今天要实现这个动画,来自 Dribbble. 实际效果: Card Animation.gif 源代码:https://github.com/seedante/CardAnimation.git 关键 ...

最新文章

  1. cmake重新编译matlab,ubuntu系统下cmake 编译matlab中mex文件
  2. linux 音频文件长度,Linux下压缩音频文件
  3. TF之DNN:对DNN神经网络进行Tensorboard可视化(得到events.out.tfevents本地服务器输出到网页可视化)
  4. Spring boot修改Servlet配置
  5. Breaking the Ledger Security Model
  6. 【CV】通俗易懂的目标检测 | RCNN, SPPNet, Fast, Faster
  7. java二维码生成并可以转换
  8. gcc -fPIC选项
  9. 【clickhouse】clickhouse 表引擎之 Merge
  10. 在本地新建分支,以进行功能开发
  11. Visual Studio 201~ Code 格式检查
  12. 编译原理 【国防科技大学网课】【笔记】【 陈火旺】 ——用于期末考试 【持续更新ing】
  13. 元胞自动机模型01——认识元细胞机模型
  14. 微信开发者工具 http 调试
  15. 小程序返回页面报错:navigateBack:fail cannot navigate back at first page.
  16. java for循环
  17. idea集成泛微axis拉取代码时报错,Axis1.4完整maven jar
  18. 3DMax软件有什么方法调节摄像机
  19. java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed
  20. kubernetes-1.23.6版本部署

热门文章

  1. 简单实现antd的表单设计
  2. 怀念偶像科比布莱恩特--------Kobe Bryant
  3. CNCC 2022| 隐私计算:理论、技术、应用与未来
  4. 金彩教育:店铺中的人才布局
  5. java毕业设计菜鸟驿站快递分发系统Mybatis+系统+数据库+调试部署
  6. U-GAT-IT:基于GAN的新型无监督图像转换
  7. 产品思维训练 | 亚马逊流量7-8月网站访客流量下降,请分析原因
  8. 有没有什么好的生日提醒软件推荐?3款软件让你的生活更有品质
  9. Python 自动化测试实战
  10. 思科模拟器启用CHAP协议