IOS UIView

原创
2014/12/17 21:43
阅读数 156

一 UIView 简述

1.UIView 相当于 容器,所有控件都可用包含在UIView内。

2.UIKit库中所有控件都直接或间接继承于UIView。

3.UIViewControler 用来管理 UIView。

#import <UIKit/UIKit.h>

//工程中ViewController 继承于 ViewController
@interface ViewController : ViewController

@end


二 UIView的常见属性

1.获得自己的父控件对象

@property(nonatomic,readonly) UIView *superview;


2.获得自己的所有子控件对象

@property(nonatomic,readonly,copy) NSArray *subviews;


3.控件的ID标识,父控件可以通过tag来找到对应的子控件

@property(nonatomic) NSInteger tag;


4.控件的形变属性,可以设置旋转角度、比例缩放、平移等属性,做动画相当重要

@property(nonatomic) CGAffineTransform transform;


5.控件所在矩形框在父控件中的位置和尺寸,以父控件的左上角为坐标原点

@property(nonatomic) CGRect frame;


6.控件所在矩形框的位置和尺寸,以自己左上角为坐标原点,所以bounds的x,y一般为0

@property(nonatomic) CGRect bounds;


7.控件中点的位置,以父控件的左上角为坐标原点

@property(nonatomic) CGPoint center;



三 UIView的常见方法

1.添加控件view

- (void)addSubview:(UIView *)view;


2.从父控件中移除

- (void)removeFromSuperview;


3.根据一个tag标识找出对应的控件,一般都是子控件

- (UIView *)viewWithTag:(NSInteger)tag;



三 工程

1 工程目录


2 storyboard 中会默认创建个view


3 代码
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //1.创建一个UI控件
    UIButton *btn = [[UIButton alloc]init];

    //2.设置名字
    [btn setTitle:@"click me!" forState:UIControlStateNormal];
    //3.设置背景颜色
    btn.backgroundColor=[UIColor redColor];
    
    //4.设置坐标
    CGFloat buttonW = 100;
    CGFloat buttonH = buttonW;
    CGFloat buttonX = (self.view.frame.size.width - buttonW) * 0.5;
    CGFloat buttonY = (self.view.frame.size.height - buttonH) * 0.5;
    btn.frame =  CGRectMake(buttonX,buttonY ,buttonW , buttonH);
    
    //5.添加到工程自带的view中
    [self.view addSubview:btn];
    
}

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

@end


4 展示


展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部