UI-UIView的介紹和案例

UIView的常見屬性


  • NSSArray *subviews

    • 所有的子控件
    • 子控件是以數(shù)組形式展現(xiàn),并且數(shù)組的順序是:越往后的控件,越在上面
  • frame

  • 控件的大小、位置(每一個控件都有frame屬性)

  • x,y值以父控件的左上角為坐標原點的

  • bounds

  • 以自己左上角為坐標原點,所以bounds的x,y為0

  • center

  • 以自己中心店角為坐標原點

UIView的常見方法


  • addSubview:
    • 添加一個子控件
  • 可以用下面的方法調(diào)整界面上的順序(view數(shù)組代碼寫在越在前面,界面顯示越在下面)
// 將子控件放在siblingSubview 的下面, 也就是界面的siblingSubview控件的上面
- (void) insertSubview:(UIView *) view belowSubview:(UIView *) siblingSubview;  
// 將子控件放在siblingSubview 的上面, 也就是界面的siblingSubview控件的上面
- (void) insertSubview:(UIView *) view aboveSubview:(UIView *) siblingSubview;  

// 將子控件放在代碼(數(shù)組)的最后面,界面上顯示最上面
- (void) bringSubviewToFront:(UIView *)view;
// 將子控件放在代碼(數(shù)組)的最后面,界面上顯示最下面
- (void) bringSubviewToBack:(UIView *)view;

UI控件概覽


IButton 按鈕
UILabel 文本標簽
UITextField 文本輸入框
UIImageView 圖片顯示
UIProgressView 進度條
UISlider 滑塊
UISwitch 開關(guān)
UISegmentControl 選項卡
UIActivityIndicator 圈圈
UIAlertView 對話框(中間彈框)
UIActionSheet 底部彈框
UIScrollView 滾動的控件
UIPageControl 分頁控件
UITextView 能滾動的文字顯示控件
UITableView 表格
UICollectionView 九宮格
UIPickerView 選擇器
UIDatePicker 日期選擇器
UIWebView 網(wǎng)頁顯示控件
UIToolbar 工具條
UINavigationBar導(dǎo)航條

案例


  • 01界面顯示 ‘添加/刪除’按鈕
  • - (void)viewDidLoad 視圖(view)加載完畢,要在界面顯示的控件。
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 創(chuàng)建一個添加按鈕
    [self addButtonWithImage:@"add" higImage:@"add_highlighted" disabledImage:@"add_disable" frame:CGRectMake(20, 20, 50, 50) tag:10 action:@selector(add)];
    
    // 創(chuàng)建一個刪除按鈕
    [self addButtonWithImage:@"remove" higImage:@"remove_highlighted" disabledImage:@"remove_disable" frame:CGRectMake(300, 20, 50, 50) tag:20 action:@selector(remove)];
    
    
}


// 簡化創(chuàng)建按鈕的代碼 “抽取代碼”

- (void)addButtonWithImage:(NSString *)image higImage:(NSString *) higImage disabledImage:(NSString *)disabledImage frame:(CGRect)frame tag:(NSInteger)tag action:(SEL)action {
    
    // 創(chuàng)建按鈕并設(shè)置按鈕屬性
    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;
    
    // 監(jiān)聽按鈕
    [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 的隱藏
    
    
    /** 九宮格 開始 */
    // 設(shè)置顯示列數(shù)
    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:當前列數(shù)。 col = 索引(index) % 設(shè)置的列數(shù)(cols)
    CGFloat col = index % cols;
    // shopsX:商品的x軸
    CGFloat shopsX = col * (shopsWitch + marginW);
    
    // 行數(shù) row = 索引數(shù)(index) 除以(/) 設(shè)置行數(shù)(cols)
    NSUInteger row = index / cols;
    // shopsY:商品的Y軸
    CGFloat shopsY = row * (shopsHeight + marginH);
  
    /** 九宮格 結(jié)束 */


    
    // 添加一個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 控制‘添加/刪除’按鈕的不可點擊狀態(tài)
  • 添加按鈕屬性
  • 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;

// 創(chuàng)建商品的數(shù)據(jù)
@property (strong, nonatomic) NSArray *shops;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 創(chuàng)建一個添加按鈕
    self.addBtn = [self addButtonWithImage:@"add" higImage:@"add_highlighted" disabledImage:@"add_disable" frame:CGRectMake(20, 20, 50, 50) tag:10 action:@selector(add)];
    
    // 創(chuàng)建一個刪除按鈕
    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;
    
    // 數(shù)據(jù)
    self.shops = @[
                   @{@"icon" : @"danjianbao", @"imageName" : @"單肩包"},
                   @{@"icon" : @"liantiaobao", @"imageName" : @"鏈條包"},
                   @{@"icon" : @"qianbao", @"imageName" : @"錢包"},
                   @{@"icon" : @"shoutibao", @"imageName" : @"手提包"},
                   @{@"icon" : @"shuangjianbao", @"imageName" : @"雙肩包"},
                   @{@"icon" : @"xiekuabao", @"imageName" : @"斜跨包"},
                   ];
}


// 簡化創(chuàng)建按鈕的代碼 “抽取代碼”

- (UIButton *)addButtonWithImage:(NSString *)image higImage:(NSString *) higImage disabledImage:(NSString *)disabledImage frame:(CGRect)frame tag:(NSInteger)tag action:(SEL)action {
    
    // 創(chuàng)建按鈕并設(shè)置按鈕屬性
    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;
    
    // 監(jiān)聽按鈕
    [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 的隱藏
    
    
    /** 九宮格 開始 */
    // 顯示列數(shù)
    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:當前列數(shù)。 col = 索引(index) % 設(shè)置的列數(shù)(cols)
    CGFloat col = index % cols;
    // shopsX:商品的x軸
    CGFloat shopsX = col * (shopsWitch + marginW);
    
    // 行數(shù) row = 索引數(shù)(index) 除以(/) 設(shè)置行數(shù)(cols)
    NSUInteger row = index / cols;
    // shopsY:商品的Y軸
    CGFloat shopsY = row * (shopsHeight + marginH);
    
    
    /** 九宮格 結(jié)束 */
    
    // 添加一個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這個界面
    
    // 每次添加按鈕都 執(zhí)行 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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,619評論 6 539
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,155評論 3 425
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,635評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,539評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,255評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,646評論 1 326
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,655評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,838評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,399評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,146評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,338評論 1 372
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,893評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,565評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,983評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,257評論 1 292
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,059評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,296評論 2 376

推薦閱讀更多精彩內(nèi)容