UIVivew

  • 创建view: UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
  • 设置颜色: view.backgroundColor = [UIColor redColor];
  • 添加view: [self.view addSubview:view];
  • 获取屏幕分辨率: self.view.bounds
  • 手机屏幕的view:self.view

模拟器快捷键

  • cmd+shift+h:home

UIView创建4部曲

  1. UIView
  2. 指明坐标
  3. 设置背景颜色
  4. 添加到背景上

UIView是UIKit框架下的,由于ViewController.h中引入了#import <UIKit/UIKit.h>,所以我们可以直接在ViewController.m中直接使用UIView

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //1.UIView
    //Frame Bounds
    //CGRect: CGPoint(x y) + CGSize(w h)
    //CGRectMake(x,y,w,h)
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
    view.backgroundColor = [UIColor redColor];
    //视图添加方法
    [self.view addSubview:view];
    CGFloat x = view.frame.origin.x;
    CGFloat y = view.frame.origin.y;
    NSLog(@"x=%f y=%f",x,y);//x=100.000000 y=100.000000
    NSLog(@"size%@",NSStringFromCGSize(view.frame.size));//size{100, 50}
    NSLog(@"frame %@",NSStringFromCGRect(view.frame)); //frame {{100, 100}, {100, 50}}
    NSLog(@"bounds %@",NSStringFromCGRect(view.bounds)); //bounds {{0, 0}, {100, 50}}
    NSLog(@"self.view.bounds:%@",NSStringFromCGRect(self.view.bounds));
    //self.view.bounds:{{0, 0}, {414, 896}} 表明当前屏幕的分辨率是414x896
    //frame是相对于父(这里的父是手机屏幕)而言的,bounds是相对于自身而言
}
@end

view的合成,最底层为手机屏幕

view的Frame和Bounds属性

frame(CGRect)是相对于父(这里的父是手机屏幕)而言的,bounds(CGRect)是相对于自身而言

CGRect = CGPoint+CGSize

view的添加有先后顺序,最后添加的在最上层,如果新添加的view看不到,那么有2种原因

  • 1,被覆盖: view的添加是有先后顺序的,最后添加的在最上层

  • 2,frame设置不正确:如手机宽度414 而你设置的是500,

results matching ""

    No results matching ""