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. python 基础干货 01

    赋值的实现 a = 'abc' 1. 在内存中创建了'abc'字符串; 2. 在内存中创建了一个名为 a 的变量, 并把它指向刚才创建的'abc', 也就是a中保存着字符串的地址. b = a, 创建 ...

  2. 什么是jetty

    转自:http://www.cnblogs.com/eafy/archive/2007/10/24/906792.html 1. 简介 Jetty 是一个开源的servlet容器,它为基于Java的w ...

  3. jquerymobile知识点:动态ListView

    这里要讲的是jqueryMobile 中的ListView 动态的列表 <ul data-role="listview" data-inset="true" ...

  4. jquerymobile知识点三:弹出层popup

    弹出层popup很简单,主要就是弹出验证,登陆注册,提交信息之类的,下面是我写好的一个demo... <div data-role="popup" id="popu ...

  5. git 命令归纳

    git 新手一枚,随用随更新 git clone git@example.com:project-name.git 克隆 git branch [-a -r] 查看分支[所有 远端] git pull ...

  6. 跟我一步一步开发自己的Openfire插件

    http://www.blogjava.net/hoojo/archive/2013/03/07/396146.html 跟我一步一步开发自己的Openfire插件 这篇是简单插件开发,下篇聊天记录插 ...

  7. [Angular 2] 8. Better ES5 Code

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. \n 与 \r

    符号 ASCII码 意义 \n 换行NL \r 回车CR 回车 \r 本义是光标重新回到本行开头,r的英文return,控制字符可以写成CR,即Carriage Return 换行 \n 本义是光标往 ...

  9. java String的比较,BOX装箱拆箱,以及面向对象的小代码

    package cn.hncu.day2; public class StringDemo { public static void main(String[] args) { String str1 ...

  10. Centos6.5 安装 MariaDB-10.0.20-linux-x86_64.tar.gz

    下载mariadb :https://downloads.mariadb.org/  我选择mariadb-10.0.20-linux-x86_64.tar.gz这个版本 复制安装文件 /opt 目录 ...