測試1佈局
ViewController測試
// ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end // ViewController.m #import "ViewController.h" @interface ViewController () /** 紅色的view*/ @property (weak, nonatomic) IBOutlet UIView *redView; /** 藍色的view*/ @property (weak, nonatomic) IBOutlet UIView *blueView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 1.建立一個橙色view UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; // 2.設置背景色 orangeView.backgroundColor = [UIColor orangeColor]; // 3.設置位置 orangeView.center = CGPointMake(175, 280); // 給self.view插入一個orangeView,而且這個orangeView是在self.blueView上的 [self.view insertSubview:orangeView aboveSubview:self.blueView]; } @end
效果圖:ui
- (void)viewDidLoad { [super viewDidLoad]; // 1.建立一個橙色view UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; // 2.設置背景色 orangeView.backgroundColor = [UIColor orangeColor]; // 3.設置位置 orangeView.center = CGPointMake(175, 280); // 給self.view插入一個orangeView,而且這個orangeView是在self.redView上的 // 默認在self.blueView的下面 [self.view insertSubview:orangeView aboveSubview:self.redView]; }
效果圖:atom
- (void)viewDidLoad { [super viewDidLoad]; // 1.建立一個橙色view UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; // 2.設置背景色 orangeView.backgroundColor = [UIColor orangeColor]; // 3.設置位置 orangeView.center = CGPointMake(175, 280); // 給self.view插入一個orangeView,而且這個orangeView是在self.blueView的下面 // 默認在self.redView上面 [self.view insertSubview:orangeView belowSubview:self.blueView]; }
效果圖:spa
- (void)viewDidLoad { [super viewDidLoad]; // 1.建立一個橙色view UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; // 2.設置背景色 orangeView.backgroundColor = [UIColor orangeColor]; // 3.設置位置 orangeView.center = CGPointMake(175, 280); // 給self.view插入一個orangeView,而且這個orangeView是在self.redView的下面 [self.view insertSubview:orangeView belowSubview:self.redView]; }
效果圖:3d
- (void)viewDidLoad { [super viewDidLoad]; // 1.建立一個橙色view UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; // 2.設置背景色 orangeView.backgroundColor = [UIColor orangeColor]; // 3.設置位置 orangeView.center = CGPointMake(175, 280); [self.view insertSubview:orangeView atIndex:0]; }
效果圖:code
- (void)viewDidLoad { [super viewDidLoad]; // 1.建立一個橙色view UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; // 2.設置背景色 orangeView.backgroundColor = [UIColor orangeColor]; // 3.設置位置 orangeView.center = CGPointMake(175, 280); [self.view addSubview:orangeView]; [orangeView bringSubviewToFront:self.blueView]; }
效果圖:orm
- (void)viewDidLoad { [super viewDidLoad]; // 1.建立一個橙色view UIView *orangeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; // 2.設置背景色 orangeView.backgroundColor = [UIColor orangeColor]; // 3.設置位置 orangeView.center = CGPointMake(175, 280); [self.view addSubview:orangeView]; // self.view有3個子控件self.redView,self.blueView,orangeView // 添加的順序爲self.redView下標爲0,self.blueView下標爲1,orangeView下標爲2 // 若是沒有下面這行代碼,那麼self.redView一定是顯示在最底部,而後是self.blueView,最後是orangeView // 可是顯示結果是self.blueView顯示在最底部.就是由於下面這句代碼 // 筆者認爲:它們三個都是self.view的子控件,sendSubviewToBack:self.blueView // 是把它們的順序排布調換了一下,即排布以後的self.blueView下標爲0,self.redView下標爲1,orangeView下標爲2,因此,綠色的view顯示在最底部了. // 相似於[self.view insertSubview:self.blueView atIndex:0];方法 [self.view sendSubviewToBack:self.blueView]; }
效果圖:blog