1. 创建iOS, Single View Application.
2. 修改YYViewController.m

//
// YYViewController.m
// DynamicViewDemo
//
// Created by yao_yu on 14-5-28.
// Copyright (c) 2014年 yao_yu. All rights reserved.
// #import "YYViewController.h" @interface YYViewController () @property(nonatomic, strong) UIView *body; @property (nonatomic, strong) UIView *currentPage; @end const CGFloat HEIGHTEDGE = 40; @implementation YYViewController -(void)createBodyView
{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect parentFrame = self.view.frame;
CGRect bodyFrame = CGRectMake(parentFrame.origin.x, parentFrame.origin.y + statusBarFrame.size.height + HEIGHTEDGE, parentFrame.size.width, parentFrame.size.height - statusBarFrame.size.height - HEIGHTEDGE);
self.body = [[UIView alloc] initWithFrame:bodyFrame];
[self.body setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:self.body];
} -(void)createCommands
{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect parentFrame = self.view.frame;
CGRect bodyFrame = CGRectMake(parentFrame.origin.x, parentFrame.origin.y + statusBarFrame.size.height, parentFrame.size.width, HEIGHTEDGE);
UIView *view = [[UIView alloc] initWithFrame:bodyFrame];
[view setBackgroundColor:[UIColor redColor]];
[self.view addSubview:view]; //添加命令按钮
const CGFloat COMMANDWIDTH = 50;
const CGFloat COMMANDHEIGHT = 30;
CGFloat midx = bodyFrame.size.width/2;
CGFloat midy = bodyFrame.size.height/2;
CGRect rect = CGRectMake(midx - COMMANDWIDTH/2, midy - COMMANDHEIGHT/2, COMMANDWIDTH, COMMANDHEIGHT);
UIButton *btnPage1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnPage1 setBackgroundColor:[UIColor clearColor]];
[btnPage1 setTitle:@"第1页" forState:UIControlStateNormal];
rect.origin.x -= COMMANDWIDTH + 20;
btnPage1.frame = rect;
[btnPage1 addTarget:self action:@selector(onShowPage1:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnPage1]; btnPage1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnPage1 setTitle:@"第2页" forState:UIControlStateNormal];
rect.origin.x += COMMANDWIDTH + 20;
btnPage1.frame = rect;
[btnPage1 addTarget:self action:@selector(onShowPage2:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnPage1]; btnPage1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnPage1 setTitle:@"第3页" forState:UIControlStateNormal];
rect.origin.x += COMMANDWIDTH + 20;
btnPage1.frame = rect;
[btnPage1 addTarget:self action:@selector(onShowPage3:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnPage1]; } -(void) onShowPage1:(id)sender
{
[self clearCurrentPage];
CGRect frame = CGRectMake(0, 0, self.body.frame.size.width, self.body.frame.size.height);
self.currentPage = [[UIView alloc] initWithFrame: frame];
[self.currentPage setBackgroundColor:[UIColor blueColor]];
[self.body addSubview:self.currentPage];
} -(void) onShowPage2:(id)sender
{
[self clearCurrentPage];
CGRect frame = CGRectMake(0, 0, self.body.frame.size.width, self.body.frame.size.height);
self.currentPage = [[UIView alloc] initWithFrame: frame];
[self.currentPage setBackgroundColor:[UIColor yellowColor]];
[self.body addSubview:self.currentPage];
} -(void) onShowPage3:(id)sender
{
[self clearCurrentPage];
CGRect frame = CGRectMake(0, 0, self.body.frame.size.width, self.body.frame.size.height);
self.currentPage = [[UIView alloc] initWithFrame: frame];
[self.currentPage setBackgroundColor:[UIColor greenColor]];
[self.body addSubview:self.currentPage];
} -(void) clearCurrentPage
{
if (self.currentPage)
[self.currentPage removeFromSuperview];
self.currentPage = nil;
} - (void)viewDidLoad
{
[super viewDidLoad];
self.currentPage = nil;
[self createBodyView];
[self createCommands];
// Do any additional setup after loading the view, typically from a nib.
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

3.运行

iOS: 学习笔记实例, 用代码控制视图创建与切换的更多相关文章

  1. IOS 学习笔记(5) 控件 文本视图(UITextView)的使用方法

    相对于UILabell所支持的较短文本内容,UITextView对于长文本的支持更好.UITextView能够以滚动的方式全部浏览到长文本,并且就像UILabel那样,从ISO6,他也提供了对NSAt ...

  2. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  3. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  4. IOS学习笔记48--一些常见的IOS知识点+面试题

      IOS学习笔记48--一些常见的IOS知识点+面试题   1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...

  5. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...

  6. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

  7. iOS学习笔记20-地图(二)MapKit框架

    一.地图开发介绍 从iOS6.0开始地图数据不再由谷歌驱动,而是改用自家地图,当然在国内它的数据是由高德地图提供的. 在iOS中进行地图开发主要有三种方式: 利用MapKit框架进行地图开发,利用这种 ...

  8. iOS学习笔记13-网络(二)NSURLSession

    在2013年WWDC上苹果揭开了NSURLSession的面纱,将它作为NSURLConnection的继任者.现在使用最广泛的第三方网络框架:AFNetworking.SDWebImage等等都使用 ...

  9. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

随机推荐

  1. linux_tomcat7服务器日志爆满导致java崩溃的问题

    在linux服务器上部署Tomcat后,运行久了,catalina.out文件会越来越大,对系统的稳定造成了一定的影响. 最近刚刚出现了某台linux服务器上的java应用都假死或挂掉 然后我在输入命 ...

  2. 查看数据库中有哪些活动的事务,对应的会话id,执行的语句

    select dbt.database_id, DB_NAME(dbt.database_id) '数据库名', dbt.transaction_id, at.name, at.transaction ...

  3. 用CSS border相关属性画三角形

    效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2l3aV9jb2Rlcg==/font/5a6L5L2T/fontsize/400/fill/I0J ...

  4. ios9中 UIStackView的使用

    ios9中 UIStackView的使用 by 伍雪颖 UIStackView能够垂直或水平排布多个subview, 自己主动为每一个subview创建和加入Auto Layout constrain ...

  5. 指向函数的指针 分类: C/C++ 2015-07-13 11:03 14人阅读 评论(0) 收藏

    原文网址:http://www.cnblogs.com/zxl2431/archive/2011/03/25/1995285.html 讲的很清楚,备份记录. (一) 用函数指针变量调用函数 可以用指 ...

  6. 晕,hibernate 的 merge和cascade="all-delete-orphan"要慎重合在一起使用

    遇到一个比较后悔莫及事情,使用了hibernate 的 merge和cascade="all-delete-orphan" ,子表数据被删除了. 1.使用cascade=" ...

  7. [React] React Fundamentals: Build a JSX Live Compiler

    we want to have the ability to write JSX and see the output live in the browser. <!doctype html&g ...

  8. visualvm

    http://visualvm.java.net/features.html http://visualvm.java.net/zh_CN/gettingstarted.html

  9. GUI编程笔记(java)01:GUI和CLI

    GUI Graphical User Interface(图形用户接口). 用图形的方式,来显示计算机操作的界面,这样更方便更直观. CLI Command line User Interface ( ...

  10. Linux如何卸载挂载文件

    在我们进行远程文件操作的时候,我们经常会出现文件服务出现卸载掉哦情况.例如 umount /mnt/net1umount: /mnt/net1: device is busy.        (In ...