UI<01>

//

//  ViewController.m

//  UIView

//

//  Created by sougu on 2017/3/13.

//  Copyright © 2017年 NextXavier. All rights reserved.

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,strong) UIView *mainView;

@property (nonatomic,strong) UIView *subView;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    

    //1、创建

    self.mainView = [[UIView alloc] init];

    //2、设置frame

    CGFloat viewX = 20;

    CGFloat viewY = 70;

    CGFloat viewW = 200;

    CGFloat viewH = 200;

    _mainView.frame = CGRectMake(viewX, viewY, viewW, viewH);

    //3、设置背景色

    _mainView.backgroundColor = [UIColor blueColor];

    

    //4、添加视图

    [self.view addSubview:_mainView];

    

    [self creatBtn];

    

}

// 创建 UIButton

- (void)creatBtn{

      //1、创建数组

    NSArray *title_arry = [NSArray arrayWithObjects:@"添加",@"隐藏",@"移除", nil];

    

    int indext = 0;

    for (int i = 0; i<title_arry.count; i++) {

        

        //2、创建按钮

        UIButton *select_btn = [UIButton buttonWithType:UIButtonTypeCustom];

        //3、设置按钮 frame

        CGFloat btnX = 20;

        CGFloat btnY = 20;

        CGFloat btnW = (self.view.frame.size.width - 20*4) / 3;

        CGFloat btnH = 30;

        select_btn.frame = CGRectMake(btnX + i * (btnX + btnW), btnY, btnW, btnH);

        //4、按钮赋 tag

        select_btn.tag = i + 100;

        //5、设置按钮边框粗细与颜色

        select_btn.layer.borderWidth = 0.5;

        select_btn.layer.borderColor = [UIColor blueColor].CGColor;

        //6、设置按钮标题和颜色

        [select_btn setTitle:[title_arry objectAtIndex:indext++] forState:UIControlStateNormal];

        [select_btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

        

        [self.view addSubview:select_btn];

        

        //7、添加事件

        [select_btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

        

        

    }

    

    

}

//点击事件

- (void)click:(UIButton *)sender{

    

    switch (sender.tag) {

        case 100:

            [self creatView];

            break;

        case 101:

            [self hideView];

            break;

        case 102:

            [self removeView];

            break;

            

        default:

            break;

    }

}

//添加

- (void)creatView{

    

    //1、创建

    self.subView = [[UIView alloc] init];

    //2、设置frame

    CGFloat viewX = 50;

    CGFloat viewY = 50;

    CGFloat viewW = 100;

    CGFloat viewH = 100;

    _subView.frame = CGRectMake(viewX, viewY, viewW, viewH);

    //3、设置背景色

    _subView.backgroundColor = [UIColor yellowColor];

    //4、添加视图

    [self.mainView addSubview:_subView];

    

    

}

//隐藏

- (void)hideView{

    //第一种:

    //[self.subView setHidden:YES];

    //第二种:

    self.subView.hidden = YES;

}

//移除

- (void)removeView{

    

    [self.subView removeFromSuperview];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

原文地址:https://www.cnblogs.com/iQingYang/p/6543216.html