iOS: 学习笔记实例, 用代码控制视图创建与切换
来源:互联网 发布:java连接mysql url 编辑:程序博客网 时间:2024/05/22 12:46
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;@endconst 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. 运行
0 0
- iOS: 学习笔记实例, 用代码控制视图创建与切换
- iOS 学习笔记 视图切换
- iOS学习笔记04-视图切换
- ios控制视图切换方向
- iOS学习笔记12--纯代码实现原生UITabBarController,手势滑动切换视图
- IOS学习笔记 - 多视图代码实现
- 【IOS学习】视图切换,纯代码实现 UITabBarController,NavigationController
- ios学习笔记之UI篇(五):切换视图
- ios开发---视图切换-storyboard创建
- iOS: 学习笔记, 用代码驱动自动布局实例
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
- iOS开发学习笔记 -- (三)动态创建视图
- iOS学习笔记——表视图一(创建)
- IOS视图切换动画效果<代码演示>
- iOS视图切换-push视图与modal视图混合使用
- iOS 视图跳转有segue连接(视图相连,非button与视图相连),代码控制跳转
- 【代码笔记】iOS-切换条
- iOS学习之——视图切换
- 再谈word2vec
- response的outputStream输出中文的乱码问题
- 二叉树遍历的递归与非递归研究
- 三层架构之基础知识
- oracle递归函数
- iOS: 学习笔记实例, 用代码控制视图创建与切换
- IO(C#) 读写文件简单操作
- 如果通过ShellExecute打开一个连接
- PHP操作mysql函数详解
- 黑马程序员-Enum使用
- C++学习第五天
- Backup and Recovery Strategies1
- Eclipse方没有提示解决办法
- java的json时间转换为年月日