VC中 自带一个view 用于铺设视图 默认颜色为透明
self.view.backgroundColor = [UIColor cyanColor];
BUTTON
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(100, 100, 100, 100);
btn.backgroundColor = [UIColor yellowColor];
[btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIImageView
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];
imgView.backgroundColor = [UIColor grayColor];
[self.view addSubview:imgView];
[imgView release];
添加图片
相对路径 修改之后仍然可以正常显示
绝对路径 如果文件位置修改 就找不到了
imgView.image = [UIImage imageNamed:@"color"];
收获路径(动态变化的绝对路径)
参数1: 文件名
参数2: 文件后缀
NSString *path = [[NSBundle mainBundle] pathForResource:@"color" ofType:@"png"];
imgView.image = [UIImage imageWithContentsOfFile:path];
圆角
imgView.layer.cornerRadius = imgView.frame.size.width / 2;
根据边界把多余部分切掉
imgView.clipsToBounds = YES;
用户名
LTView *user = [[LTView alloc] initWithFrame:CGRectMake(0, 250, 375, 60)];
user.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:user];
[user release];
user.label.text = @"用户名";
页面跳转(模态)
- (void)go
{
NSLog(@"点点点");
TwoViewController *twoVC = [[TwoViewController alloc] init];
[self presentViewController:twoVC animated:YES completion:^{
}];
}
模态返回
- (void)back
{
NSLog(@"返回");
[self dismissViewControllerAnimated:YES completion:^{
}];
}
导航
导航栏设置: controller(栏)/item(栏上的元素)
导航栏显示/隐藏
self.navigationController.navigationBarHidden = NO;
或 self.navigationController.navigationBar.hidden = YES;
栏样式
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
半透明效果
开始效果时 屏幕左上角为坐标原点
关闭时 导航栏的左下角为坐标原点
self.navigationController.navigationBar.translucent = YES;
创建view(0,0,100,100)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
[view release];
栏背景颜色
self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
栏颜色
self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
栏标题
self.title = @"这是一个标题";
self.navigationItem.title = @"这是一个猴赛雷的标题";
分段控制器
UISegmentedControl *seg = [[[UISegmentedControl alloc] initWithItems:@[@"消息", @"电话"]] autorelease];
seg.frame = CGRectMake(0, 0, 100, 30);
栏标题视图
self.navigationItem.titleView = seg;
栏左侧按钮
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(left:)] autorelease];
栏右侧按钮
系统按钮样式
UIBarButtonItem *b1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(right1)] autorelease];
自定义按钮图片
UIBarButtonItem *b2 = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"dianzan"] style:UIBarButtonItemStylePlain target:self action:@selector(right2)] autorelease];
self.navigationItem.rightBarButtonItems = @[b1, b2];
修改导航栏上内容的颜色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
跳转页面
写一个button
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(200, 200, 100, 100);
btn.backgroundColor = [UIColor yellowColor];
[self.view addSubview:btn];
调用goTwo方法
[btn addTarget:self action:@selector(goTwo) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 跳转页面
- (void)goTwo
{
1.获取第二页对象
TwoViewController *twoVC = [[TwoViewController alloc] init];
2.跳转(由导航控制器 从当前push到第二页)
[self.navigationController pushViewController:twoVC animated:YES];
3.内存管理
[twoVC release];
}
页面返回
- (void)back
{
NSLog(@"返回");
由导航控制器 控制当前VC返回上一页
[self.navigationController popViewControllerAnimated:YES];
}
页面间传值
协议4.签协议
@interface RootViewController ()<PassDelegate>
@property (nonatomic, retain) UITextField *tf1;
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"首页";
self.tf1 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
_tf1.backgroundColor = [UIColor yellowColor];
[self.view addSubview:_tf1];
[_tf1 release];
UIButton *go = [UIButton buttonWithType:UIButtonTypeSystem];
go.frame = CGRectMake(100, 200, 200, 40);
[go setTitle:@"GO" forState:UIControlStateNormal];
go.backgroundColor = [UIColor redColor];
[go addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:go];
调用的方法
- (void)go
{
TwoViewController *twoVC = [[TwoViewController alloc] init];
属性2: 在push页面之前传值(创建对象之后 push之前)
twoVC.string = self.f1.text;
协议5: 设置代理人
为了保证 设置代理人的对象和push的对象是同一个 在创建对象之后 push之前 设置delegate
twoVC.delegate = self;
[self.navigationController pushViewController:twoVC animated:YES];
[twoVC release];
}
协议6: 实现协议方法
- (void)passValue:(NSString *)string
{
把收到的string 赋值给输入框
self.tf1.text = string;
}
TwoViewController.h中
协议1: 声明协议(定义一个带参数的方法)
@protocol PassDelegate<NSObject>
@optional
@required(默认)
- (void)passValue:(NSString *)string;
@end
@interface TwoViewController : UIViewController
属性1: 在第二页声明一个属性 用来保存数据
@property (nonatomic, copy) NSString *string;
协议2: 定义代理人属性@property (nonatomic, assign) id<PassDelegate>delegate;
@end
TwoViewController.m中
@property (nonatomic, retain) UITextField *tf2;
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"第二页";
self.tf2 = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 40)];
_tf2.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_tf2];
[_tf2 release];
属性3: 通过属性给当前页内容赋值
self.tf2.text = self.string;
UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem];
back.frame = CGRectMake(100, 200, 200, 40);
[back setTitle:@"BACK" forState:UIControlStateNormal];
back.backgroundColor = [UIColor greenColor];
[back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:back];
}
- (void)back
{
协议3: 返回上一页之前 让代理人调用协议方法
[self.delegate passValue:self.tf2.text];
[self.navigationController popViewControllerAnimated:YES];
}