在UI的第一个.m文件中
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 创建window,让其充满屏幕
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
// 让window成为主窗口且可视
[self.window makeKeyAndVisible];
// 设置背景色
self.window.backgroundColor = [UIColor blueColor];
// 设置根视图控制器
self.window.rootViewController = [[ViewController alloc ]init];
// Override point for customization after application launch.
return YES;
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
在第二个UI.m文件中的程序
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor =[UIColor brownColor];
UIView *v1 = [[UIView alloc]init];
UIView *v3 = [[UIView alloc]init];
UIView *v5 = [[UIView alloc]init];
UIView *v6 = [[UIView alloc]init];
// 相对于父视图的位置,注意坐标和尺寸的合理性,保证坐标加尺寸不会超出父视图范围
v6.frame = CGRectMake(100, 250, 100, 100);
v6.backgroundColor = [UIColor orangeColor];
[self.view addSubview:v6];
UIView *v7 = [[UIView alloc]init];
v7.frame = CGRectMake(50, 350, 50, 50);
v7.backgroundColor = [UIColor orangeColor];
[self.view addSubview:v7];
v5.frame = CGRectMake(100, 100, -50, -50);
v5.backgroundColor = [UIColor purpleColor];
[self.view addSubview:v5];
v3.frame = CGRectMake( 200, 200, 50, 50);
v3.backgroundColor = [UIColor yellowColor];
[self.view addSubview:v3];
// [v3 removeFromSuperview];
v3.userInteractionEnabled = NO;
v1.frame = CGRectMake(100, 100, 100, 100);
v1.backgroundColor = [UIColor blueColor];
[self.view addSubview:v1];
// 将后面的视图添加到前面的视图之上
// [self.view insertSubview:v1 atIndex:2];//将子视图添加到父视图的某个位置
[self.view insertSubview:v3 atIndex:3];
// [self.view insertSubview:v3 aboveSubview:v1];
// 将v3添加到父视图,且在v1之上
// [self.view insertSubview:v3 belowSubview:v1];
// 将v3添加到父视图,且在v1之下
// [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
// 交换两个位置的视图
// [self.view bringSubviewToFront:v1];
//将某个子视图移到父视图的最前方
[self.view sendSubviewToBack:v3];
// 将某个子视图移到父视图的最底层
// 是否允许用户点击(默认YES),如果设置成no,子视图不会覆盖父视图的点击事件
v1.userInteractionEnabled = NO;
// self.view.userInteractionEnabled = NO;
// 如果父视图不允许交互,那么子视图的事件也会被屏蔽
// v3.tag = 2 ;
// 设置视图的标签
// v3.alpha = 1;
// 设置视图的透明度,0~1浮点
// self.view.alpha = 0;
// 如果父视图透明,那么子视图也会看不见
// v1.tag = 1 ;
//
// v1.alpha = 1;
// UIView *v4 = [self.view viewWithTag:2];
// v4.backgroundColor = [UIColor redColor];
// UIView *v2 = [self.view viewWithTag:1];
// 获取父视图中标签为1的视图
//
// v2.backgroundColor = [UIColor redColor];
// v1.hidden = NO;
// 设置视图是否隐藏(默认NO)
// self.view.hidden = YES;
// 如果父视图被隐藏,那么子视图也会被隐藏
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"被点击");
}
对于初学者来说,要记很多的知识是必不可少的,只有积累的够多你才能用的时候更加灵活。