IOS界面开发之UIView

前面学习的Objective-C或Swift都是一些语法,与真正的IOS开发还是有差别的,它们的关系就跟Java与Android一样,会Java不一定都会开发Android,在这里会使用Objective-C或Swift不一定都会开发IOS,IOS还要学习很多的框架

Foundation 其它框架都是建立在这个框架之上的,它提供一些数据操作类。

UIKit 提供创建基于触摸界人士的类。

Core Data 管理应用数据模型

Core Graphics 图形处理框架

Core Animation 动画和虚拟效果框架

OpenGL ES 提供2D和3D绘图工具

接下来就学习下UIKit框架的一些类的使用,从最基础的类开始,UIView是很多控件类的父类,但不是所有控件类的父类,这点一定要明白。

下面这段代码使用了UIView的一些属性,和通过UIButton启动另一个界面。

//
//  ViewController.m
//  UIViewDemo
//
//  Created by dcr on 2016/12/12.
//  Copyright © 2016年 All rights reserved.
//

#import "ViewController.h"
#import "ImageViewController.h"
#import "LabelViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //状态栏的大小
    CGRect rect = [[UIApplication sharedApplication] statusBarFrame];
    //屏幕大小
    float width = [[UIScreen mainScreen] bounds].size.width;
    float height = [[UIScreen mainScreen] bounds].size.height;
    NSLog(@"width = %f height = %f \n status bar w = %f h = %f", width, height, rect.size.width, rect.size.height);
    //导航栏的大小
    //CGRect rectNav = self.navigationController.navigationBar.frame;
    //NSLog(@"rectNav w = %f h = %f", rectNav.size.width, rectNav.size.height);
    UIView *view1 = [[UIView alloc] init];
    view1.frame = CGRectMake(10, rect.size.height + 10, width - 20, height - rect.size.height - 20);
    NSLog(@"frame x = %f y = %f w = %f h = %f", view1.frame.origin.x, view1.frame.origin.y, view1.frame.size.width, view1.frame.size.height);
    //bounds是边框大小,它的x y值永远都为0
    NSLog(@"bounds x = %f y = %f w = %f h = %f", view1.bounds.origin.x, view1.bounds.origin.y, view1.bounds.size.width, view1.bounds.size.height);
    NSLog(@"center x = %f y = %f", view1.center.x, view1.center.y);
    view1.backgroundColor = [UIColor redColor];
    [self.view addSubview:view1];
    
    //父视图
    UIView *superView = view1.superview;
    superView.backgroundColor = [UIColor greenColor];
    
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor blueColor];
    view2.frame = CGRectMake(10, 150, 50, 50);
    view2.tag = 2;
    [view1 addSubview:view2];
    
    UIView *view3 = [[UIView alloc] init];
    view3.backgroundColor = [UIColor purpleColor];
    view3.frame = CGRectMake(20, 130, 30, 100);
    view3.tag = 3;
    [view1 addSubview:view3];
    
    //子视图
    NSArray *subArray = view1.subviews;
    for(UIView *view in subArray){
        if(view.tag == 3)
            view.backgroundColor = [UIColor whiteColor];
    }
    //通过Tag得到字视图
    UIView *subView = [view1 viewWithTag:2];
    subView.backgroundColor = [UIColor greenColor];
    //同级视图处理,同一个父视图中先加入的View会被盖在下面
    //UIView *view4 = [[UIView alloc] init];
    //view4.backgroundColor = [UIColor purpleColor];
    //view4.frame = CGRectMake(40, 110, 100, 200);
    //view4.tag = 4;
    //[self.view addSubview:view4];
    
    //交换两个层的视图
    [view1 exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    //插入视图到指定层
    UIView *view5 = [[UIView alloc] init];
    view5.backgroundColor = [UIColor blackColor];
    view5.frame = CGRectMake(45, 110, 30, 100);
    view5.tag = 5;
    //[view1 insertSubview:view5 atIndex:1];
    [view1 insertSubview:view5 aboveSubview:view2];
    //[view1 insertSubview:view5 belowSubview:view2];
    //将一个视图放入最低层或最顶层
    //最顶层
    //[view1 bringSubviewToFront:view5];
    //最低层
    [view1 sendSubviewToBack:view5];
    
    
    //自适应
    UIView *centerView = [[UIView alloc] init];
    centerView.backgroundColor = [UIColor purpleColor];
    centerView.frame = CGRectMake(width/2 - 25, height/2 - 25 - rect.size.height, 50, 50);
    centerView.autoresizesSubviews = YES;
    centerView.tag = 1001;
    [view1 addSubview:centerView];
    
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor redColor];
    topView.frame = CGRectMake(10, 10, 30, 30);
    topView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleTopMargin|
    UIViewAutoresizingFlexibleBottomMargin|
    UIViewAutoresizingFlexibleWidth|
    UIViewAutoresizingFlexibleHeight;
    [centerView addSubview:topView];
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    addBtn.frame = CGRectMake(50, height/2 - rect.size.height + 75, 100, 40);
    addBtn.backgroundColor = [UIColor whiteColor];
    addBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [addBtn setTitle:@"变大" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
    
    
    UIButton *sbuBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    sbuBtn.frame = CGRectMake(200, height/2 - rect.size.height + 75, 100, 40);
    sbuBtn.backgroundColor = [UIColor whiteColor];
    sbuBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sbuBtn setTitle:@"变小" forState:UIControlStateNormal];
    [sbuBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sbuBtn addTarget:self action:@selector(subClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sbuBtn];
    
    //测试UILabel控件
    UIButton *btnLabel = [UIButton buttonWithType:UIButtonTypeSystem];
    btnLabel.frame = CGRectMake(50, height/2 - rect.size.height + 130, 100, 40);
    btnLabel.backgroundColor = [UIColor whiteColor];
    btnLabel.titleLabel.font = [UIFont systemFontOfSize:16];
    [btnLabel setTitle:@"使用UILabel" forState:UIControlStateNormal];
    [btnLabel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnLabel addTarget:self action:@selector(btnLabelClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLabel];
    
    //测试UIImageView控件
    UIButton *btnImageView = [UIButton buttonWithType:UIButtonTypeSystem];
    btnImageView.frame = CGRectMake(50, height/2 - rect.size.height + 190, 200, 40);
    btnImageView.backgroundColor = [UIColor whiteColor];
    btnImageView.titleLabel.font = [UIFont systemFontOfSize:16];
    [btnImageView setTitle:@"使用UIImageView" forState:UIControlStateNormal];
    [btnImageView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btnImageView addTarget:self action:@selector(btnImageViewClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnImageView];
}

/**
 控件适应变大
 */
- (void)addClick{
    UIView *view = [self.view viewWithTag:1001];
    view.frame = CGRectMake(view.frame.origin.x-5, view.frame.origin.y-5, view.frame.size.width+10, view.frame.size.height+10);
}

/**
 控件适应变小
 */
- (void)subClick{
    UIView *view = [self.view viewWithTag:1001];
    view.frame = CGRectMake(view.frame.origin.x+5, view.frame.origin.y+5, view.frame.size.width-10, view.frame.size.height-10);
}

/**
 启动LabelViewController
 */
- (void)btnLabelClick{
    //通过代码启动另一个界面
    [self presentViewController:[[LabelViewController alloc] init] animated:true completion:^{
        //启动完成回调
        NSLog(@"LabelViewController start completion");
    }];
}

/**
 启动ImageViewController
 */
- (void)btnImageViewClick{
    //通过代码启动另一个界面
    [self presentViewController:[[ImageViewController alloc] init] animated:true completion:^{
        //启动完成回调
        NSLog(@"ImageViewController start completion");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

完整示例代码下载:http://download.csdn.net/detail/deng0zhaotai/9715127


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值