iOS: 学习笔记实例, 用代码控制视图创建与切换
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: 学习笔记实例, 用代码控制视图创建与切换的更多相关文章
- IOS 学习笔记(5) 控件 文本视图(UITextView)的使用方法
相对于UILabell所支持的较短文本内容,UITextView对于长文本的支持更好.UITextView能够以滚动的方式全部浏览到长文本,并且就像UILabel那样,从ISO6,他也提供了对NSAt ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- IOS学习笔记48--一些常见的IOS知识点+面试题
IOS学习笔记48--一些常见的IOS知识点+面试题 1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...
- iOS学习笔记10-UIView动画
上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...
- iOS学习笔记-自定义过渡动画
代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...
- iOS学习笔记20-地图(二)MapKit框架
一.地图开发介绍 从iOS6.0开始地图数据不再由谷歌驱动,而是改用自家地图,当然在国内它的数据是由高德地图提供的. 在iOS中进行地图开发主要有三种方式: 利用MapKit框架进行地图开发,利用这种 ...
- iOS学习笔记13-网络(二)NSURLSession
在2013年WWDC上苹果揭开了NSURLSession的面纱,将它作为NSURLConnection的继任者.现在使用最广泛的第三方网络框架:AFNetworking.SDWebImage等等都使用 ...
- iOS学习笔记——AutoLayout的约束
iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...
随机推荐
- TortoiseSVN客户端重新设置用户名和密码[转]
在第一次使用TortoiseSVN从服务器CheckOut的时候,会要求输入用户名和密码,这时输入框下面有个选项是保存认证信息,如果选了这个选项,那么以后就不用每次都输入一遍用户名密码了. 不过,如果 ...
- 在mysql中修改表名的sql语句
在使用mysql时,经常遇到表名不符合规范或标准,但是表里已经有大量的数据了,如何保留数据,只更改表名呢?可以通过建一个相同的表结构的表,把原来的数据导入到新表中,但是这样视乎很麻烦.能否简单使用一个 ...
- 8 Pratical Examples of Linux “Touch” Command--reference
In Linux every single file is associated with timestamps, and every file stores the information of l ...
- thinking in java 读书笔记 --- overriding private method
一个对象可以被用作它自身的类型或者是它的基类类型.当用作它的基类类型时叫做upcasting,如下所示 BaseClass ref = new DerivedClass() //upcasting ...
- Android 自定义View修炼-自定义HorizontalScrollView视图实现仿ViewPager效果
开发过程中,需要达到 HorizontalScrollView和ViewPager的效果,于是直接重写了HorizontalScrollView来达到实现ViewPager的效果. 实际效果图如下: ...
- centos6.7 install chrome
1.yum仓库 (如果用rpm包安装 可以忽略此步) vim /etc/yum.repos.d/google-chrome.repo [google-chrome] name=google-chrom ...
- Silverlight实用窍门系列:47.Silverlight中元素到元素的绑定,以及ObservableCollection和List的使用区别
问题一:在某一些情况下,我们使用MVVM模式的时候,对于某一个字段(AgeField)需要在前台的很多个控件(A.B.C.D.E)进行绑定,但是如何能够让我们后台字段名改变的时候能够非常方便的改变所有 ...
- 绝对定位的DIV绝对居中显示
绝对居中:即在客户端上任何分辨率下纵横方向均居中 紫色的正方形为绝对定位的div position:absolute; top: 50%; left: 50%; 只能把div定位在以红色圈为起点的位置 ...
- Java-Android 之动画的实现
一:显示隐藏动画 在res目录下创建一个anim目录,然后在里面创建一个alpha.xml文件 <?xml version="1.0" encoding="utf- ...
- Orcale安装完成后 em管理、性能无法登陆 报:没有找到主机
先在我的电脑环境变量中加入oracle_sid=orcl 在Orcale主目录中查找emd.properties 文件修改(时间格式) agentTZRegion=GMT agentTZRegion= ...