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. JSTL 数字日期标签库

    <frm:formatNumber/>标签 该标签依据特定的区域将数字改变为不同的格式来显示. 被格式化的数据<frm:formatNumber> <fmt:format ...

  2. SQL SERVER 中如何将NULL转换为0

    select isnull(fieldname,0) from tablename 如果字段fieldname的值是null,则结果是0

  3. [D3 + AngularJS] 15. Create a D3 Chart as an Angular Directive

    Integrating D3 with Angular can be very simple. In this lesson, you will learn basic integration as ...

  4. A Cross-Platform Memory Leak Detector

    Memory leakage has been a permanent annoyance for C/C++ programmers. Under MSVC, one useful feature ...

  5. 浅谈Android自定义锁屏页的发车姿势

    一.为什么需要自定义锁屏页 锁屏作为一种黑白屏时代就存在的手机功能,至今仍发挥着巨大作用,特别是触屏时代的到来,锁屏的功用被发挥到了极致.多少人曾经在无聊的时候每隔几分钟划开锁屏再关上,孜孜不倦,其酸 ...

  6. HTML5本地化应用开发-HTML5 Web存储详解

    文章不是简单的的Ctrl C与V,而是一个字一个标点符号慢慢写出来的.我认为这才是是对读者的负责,本教程由技术爱好者成笑笑(博客:http://www.chengxiaoxiao.com/)写作完成. ...

  7. mysql-distinct去重、mysql-group&nbsp;…

    一.MYSQL-distinct用法 在使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记 ...

  8. css修改li前面的小圆点的颜色

    直接改 li 的style 的 color 就好:就是这样:over

  9. .Net之美读书系列(二):委托进阶

    这次看书的知识点: 事件访问器 如果一个委托中注册了多个事件且需要获取其返回值的方法 委托的异常处理 委托处理超时的方法 异步委托 事件访问器 职能有: 1.对委托属性进行封装,不再直接该委托变量直接 ...

  10. ubuntu下配置java环境变量

    1.官网下载linux对应的jdk安装包tar.gz 2.filezilla上传tar.gz到对应ubuntu目录test下(见上一篇) 3.解压:tar -zcvf XXX.tar.gz 4.修改解 ...