UI-UIView的介绍和案例

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,242评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,769评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,484评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,133评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,007评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,080评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,496评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,190评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,464评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,549评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,330评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,205评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,567评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,889评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,160评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,475评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,650评论 2 335

推荐阅读更多精彩内容