UIView的常见属性
-
NSSArray *subviews
- 所有的子控件
- 子控件是以数组形式展现,并且数组的顺序是:越往后的控件,越在上面
frame
控件的大小、位置(每一个控件都有frame属性)
x,y值以父控件的左上角为坐标原点的
bounds
以自己左上角为坐标原点,所以bounds的x,y为0
center
以自己中心店角为坐标原点
UIView的常见方法
- addSubview:
- 添加一个子控件
- 可以用下面的方法调整界面上的顺序(view数组代码写在越在前面,界面显示越在下面)
// 将子控件放在siblingSubview 的下面, 也就是界面的siblingSubview控件的上面
- (void) insertSubview:(UIView *) view belowSubview:(UIView *) siblingSubview;
// 将子控件放在siblingSubview 的上面, 也就是界面的siblingSubview控件的上面
- (void) insertSubview:(UIView *) view aboveSubview:(UIView *) siblingSubview;
// 将子控件放在代码(数组)的最后面,界面上显示最上面
- (void) bringSubviewToFront:(UIView *)view;
// 将子控件放在代码(数组)的最后面,界面上显示最下面
- (void) bringSubviewToBack:(UIView *)view;
UI控件概览
IButton 按钮
UILabel 文本标签
UITextField 文本输入框
UIImageView 图片显示
UIProgressView 进度条
UISlider 滑块
UISwitch 开关
UISegmentControl 选项卡
UIActivityIndicator 圈圈
UIAlertView 对话框(中间弹框)
UIActionSheet 底部弹框
UIScrollView 滚动的控件
UIPageControl 分页控件
UITextView 能滚动的文字显示控件
UITableView 表格
UICollectionView 九宫格
UIPickerView 选择器
UIDatePicker 日期选择器
UIWebView 网页显示控件
UIToolbar 工具条
UINavigationBar导航条
案例
- 01界面显示 ‘添加/删除’按钮
-
- (void)viewDidLoad
视图(view)加载完毕,要在界面显示的控件。
- (void)viewDidLoad {
[super viewDidLoad];
// 创建一个添加按钮
[self addButtonWithImage:@"add" higImage:@"add_highlighted" disabledImage:@"add_disable" frame:CGRectMake(20, 20, 50, 50) tag:10 action:@selector(add)];
// 创建一个删除按钮
[self addButtonWithImage:@"remove" higImage:@"remove_highlighted" disabledImage:@"remove_disable" frame:CGRectMake(300, 20, 50, 50) tag:20 action:@selector(remove)];
}
// 简化创建按钮的代码 “抽取代码”
- (void)addButtonWithImage:(NSString *)image higImage:(NSString *) higImage disabledImage:(NSString *)disabledImage frame:(CGRect)frame tag:(NSInteger)tag action:(SEL)action {
// 创建按钮并设置按钮属性
UIButton *btn = [[UIButton alloc]init];
[btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:higImage] forState:UIControlStateHighlighted];
[btn setBackgroundImage:[UIImage imageNamed:disabledImage] forState:UIControlStateDisabled];
btn.frame = frame;
// 监听按钮
[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
// Tag
btn.tag = tag;
// 显示在 View 视图中
[self.view addSubview:btn];
}
- 02 ‘添加/删除’按钮的 方法
/** 添加按钮 方法*/
- (void)add {
// 添加一个modeView 的块。把图片和文字控件添加到modeView,这样就能只算modeView的x,y轴了
UIView *modeView = [[UIView alloc]init];
modeView.backgroundColor = [UIColor redColor];
modeView.frame = CGRectMake(shopsX, shopsY, shopsWitch, shopsHeight);
[self.shopView addSubview:modeView];
// 添加图片
UIImageView *iconIamge = [[UIImageView alloc]init];
iconIamge.image = [UIImage imageNamed:@"danjianbao"];
iconIamge.frame = CGRectMake(0, 0, shopsWitch, shopsWitch);
[modeView addSubview:iconIamge];
// 添加按钮
UILabel *label = [[UILabel alloc]init];
label.text = @"DanJan";
label.frame = CGRectMake(0, shopsWitch, shopsWitch, shopsHeight-shopsWitch);
label.font = [UIFont systemFontOfSize:11];
label.textAlignment = NSTextAlignmentCenter;
[modeView addSubview:label]; // 显示到modeVeiw这个界面
NSLog(@"add---");
}
/** 删除按钮 方法*/
- (void)remove {
NSLog(@"remove---");
}
- 03 九宫格
/** 添加按钮 方法*/
- (void)add {
// self.shopView.clipsToBounds = YES; // 超出 shopView 的隐藏
/** 九宫格 开始 */
// 设置显示列数
int cols = 4;
CGFloat shopsWitch = 50; // 商品的宽度
CGFloat shopsHeight = 70; // 商品的高度:图片+文字
// 商品间隔的宽度
CGFloat marginW = (self.shopView.frame.size.width - cols * shopsWitch) / (cols -1);
// 商品间隔的高度
CGFloat marginH = 20;
//index索引不能为负值,所以要用NSUInteger,用int 会报错。添加一个modeView count为0。再添加一个modeView count为1。
NSUInteger index = self.shopView.subviews.count; // subviews 是NSArray类型
// CGFloat:浮点型; col:当前列数。 col = 索引(index) % 设置的列数(cols)
CGFloat col = index % cols;
// shopsX:商品的x轴
CGFloat shopsX = col * (shopsWitch + marginW);
// 行数 row = 索引数(index) 除以(/) 设置行数(cols)
NSUInteger row = index / cols;
// shopsY:商品的Y轴
CGFloat shopsY = row * (shopsHeight + marginH);
/** 九宫格 结束 */
// 添加一个modeView 的块。把图片和文字控件添加到modeView,这样就能只算modeView的x,y轴了
UIView *modeView = [[UIView alloc]init];
modeView.backgroundColor = [UIColor redColor];
modeView.frame = CGRectMake(shopsX, shopsY, shopsWitch, shopsHeight);
[self.shopView addSubview:modeView];
// 添加图片
UIImageView *iconIamge = [[UIImageView alloc]init];
iconIamge.image = [UIImage imageNamed:@"danjianbao"];
iconIamge.frame = CGRectMake(0, 0, shopsWitch, shopsWitch);
[modeView addSubview:iconIamge];
// 添加按钮
UILabel *label = [[UILabel alloc]init];
label.text = @"DanJan";
label.frame = CGRectMake(0, shopsWitch, shopsWitch, shopsHeight-shopsWitch);
label.font = [UIFont systemFontOfSize:11];
label.textAlignment = NSTextAlignmentCenter;
[modeView addSubview:label]; // 显示到modeVeiw这个界面
NSLog(@"add---");
}
- 04 控制‘添加/删除’按钮的不可点击状态
- 添加按钮属性
-
self.removeBtn.enabled = NO;
按钮不可点击
/** 添加按钮 */
@property (weak,nonatomic) UIButton *addBtn;
/** 删除按钮 */
@property (weak,nonatomic) UIButton *removeBtn;
// if (self.shopView.subviews.count == self.shops.count) {
// self.addBtn.enabled = NO;
// }else{
// self.addBtn.enabled =YES;
// }
//
self.addBtn.enabled = (self.shopView.subviews.count < self.shops.count);
// if (self.shopView.subviews.count == 0) {
// self.removeBtn.enabled = NO;
// }else{
// self.removeBtn.enabled = YES;
// }
self.removeBtn.enabled = (self.shopView.subviews.count > 0);
- 全部代码
//
// ViewController.m
// 02-UIView2
//
// Created by xcode on 16/1/30.
// Copyright (c) 2016年 xcode. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
// 弹出框
@property (weak, nonatomic) IBOutlet UILabel *hud;
@property (weak, nonatomic) IBOutlet UIView *shopView;
/** 添加按钮 */
@property (weak,nonatomic) UIButton *addBtn;
/** 删除按钮 */
@property (weak,nonatomic) UIButton *removeBtn;
// 创建商品的数据
@property (strong, nonatomic) NSArray *shops;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建一个添加按钮
self.addBtn = [self addButtonWithImage:@"add" higImage:@"add_highlighted" disabledImage:@"add_disable" frame:CGRectMake(20, 20, 50, 50) tag:10 action:@selector(add)];
// 创建一个删除按钮
self.removeBtn = [self addButtonWithImage:@"remove" higImage:@"remove_highlighted" disabledImage:@"remove_disable" frame:CGRectMake(300, 20, 50, 50) tag:20 action:@selector(remove)];
self.removeBtn.enabled = NO;
// 数据
self.shops = @[
@{@"icon" : @"danjianbao", @"imageName" : @"单肩包"},
@{@"icon" : @"liantiaobao", @"imageName" : @"链条包"},
@{@"icon" : @"qianbao", @"imageName" : @"钱包"},
@{@"icon" : @"shoutibao", @"imageName" : @"手提包"},
@{@"icon" : @"shuangjianbao", @"imageName" : @"双肩包"},
@{@"icon" : @"xiekuabao", @"imageName" : @"斜跨包"},
];
}
// 简化创建按钮的代码 “抽取代码”
- (UIButton *)addButtonWithImage:(NSString *)image higImage:(NSString *) higImage disabledImage:(NSString *)disabledImage frame:(CGRect)frame tag:(NSInteger)tag action:(SEL)action {
// 创建按钮并设置按钮属性
UIButton *btn = [[UIButton alloc]init];
[btn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:higImage] forState:UIControlStateHighlighted];
[btn setBackgroundImage:[UIImage imageNamed:disabledImage] forState:UIControlStateDisabled];
btn.frame = frame;
// 监听按钮
[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
// Tag
btn.tag = tag;
// 显示在 View 视图中
[self.view addSubview:btn];
return btn;
}
/** 添加按钮 方法*/
- (void)add {
// self.shopView.clipsToBounds = YES; // 超出 shopView 的隐藏
/** 九宫格 开始 */
// 显示列数
int cols = 3;
CGFloat shopsWitch = 80; // 商品的宽度
CGFloat shopsHeight = 100; // 商品的高度:图片+文字
// 商品间隔的宽度
CGFloat marginW = (self.shopView.frame.size.width - cols * shopsWitch) / (cols -1);
// 商品间隔的高度
CGFloat marginH = 20;
//index索引不能为负值,所以要用NSUInteger,用int 会报错。添加一个modeView count为0。再添加一个modeView count为1。
NSUInteger index = self.shopView.subviews.count; // subviews 是NSArray类型
// CGFloat:浮点型; col:当前列数。 col = 索引(index) % 设置的列数(cols)
CGFloat col = index % cols;
// shopsX:商品的x轴
CGFloat shopsX = col * (shopsWitch + marginW);
// 行数 row = 索引数(index) 除以(/) 设置行数(cols)
NSUInteger row = index / cols;
// shopsY:商品的Y轴
CGFloat shopsY = row * (shopsHeight + marginH);
/** 九宫格 结束 */
// 添加一个modeView 的块。把图片和文字控件添加到modeView,这样就能只算modeView的x,y轴了
UIView *modeView = [[UIView alloc]init];
modeView.backgroundColor = [UIColor redColor];
modeView.frame = CGRectMake(shopsX, shopsY, shopsWitch, shopsHeight);
[self.shopView addSubview:modeView];
NSDictionary *shop = self.shops[index];
// 添加图片
UIImageView *iconIamge = [[UIImageView alloc]init];
iconIamge.image = [UIImage imageNamed:shop[@"icon"]];
iconIamge.frame = CGRectMake(0, 0, shopsWitch, shopsWitch);
[modeView addSubview:iconIamge];
// 添加按钮
UILabel *label = [[UILabel alloc]init];
label.text = shop[@"imageName"];
label.frame = CGRectMake(0, shopsWitch, shopsWitch, shopsHeight-shopsWitch);
label.font = [UIFont systemFontOfSize:11];
label.textAlignment = NSTextAlignmentCenter;
[modeView addSubview:label]; // 显示到modeVeiw这个界面
// 每次添加按钮都 执行 checkState 这个方法检查 shopsView 有多少个
[self checkState];
// if (self.shopView.subviews.count == self.shops.count) {
// // 添加满了
// self.addBtn.enabled = NO;
// }
//
// self.removeBtn.enabled = YES;
NSLog(@"add---");
}
/** 删除按钮 方法*/
- (void)remove {
[[self.shopView.subviews lastObject] removeFromSuperview];
[self checkState];
// if (self.shopView.subviews.count == 0) {
// self.removeBtn.enabled = NO;
// }
// self.addBtn.enabled = YES;
//
NSLog(@"remove---");
}
- (void)checkState {
// if (self.shopView.subviews.count == self.shops.count) {
// self.addBtn.enabled = NO;
// }else{
// self.addBtn.enabled =YES;
// }
//
self.addBtn.enabled = (self.shopView.subviews.count < self.shops.count);
// if (self.shopView.subviews.count == 0) {
// self.removeBtn.enabled = NO;
// }else{
// self.removeBtn.enabled = YES;
// }
self.removeBtn.enabled = (self.shopView.subviews.count > 0);
// if (self.shopView.subviews.count == self.shops.count) {
// // 添加满了
// self.addBtn.enabled = NO;
// }
//
// self.removeBtn.enabled = YES;
//
//
//
// if (self.shopView.subviews.count == 0) {
// self.removeBtn.enabled = NO;
// }
// self.addBtn.enabled = YES;
if (self.addBtn.enabled == NO) {
self.hud.alpha = 1.0;
self.hud.text = @"添加满了!";
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.hud.alpha = 0.0;
});
}else if (self.removeBtn.enabled == NO) {
self.hud.alpha = 1.0;
self.hud.text = @"没有了";
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.hud.alpha = 0.0;
});
}
}
@end