第四课:在UIViewController上添加 UIView UIImageView UILabel。这节demo我们学习在UIViewController的View上添加 UIView UIImageView UILabel。
//在上一个Demo中我们学习了如何创建ViewController
//这里我们学习下如何添加 UIView UIImageView UILabel
具体见 ViewController.m文件中 viewDidLoad函数
AppDelegate.h
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@implementation AppDelegate
<span style="font-family: Arial, Helvetica, sans-serif;">// 当应用程序启动完毕的时候就会调用(系统自动调用)</span>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
// 即将失去活动状态的时候调用(失去焦点, 不可交互)
- (void)applicationWillResignActive:(UIApplication *)application
{
NSLog(@"ResignActive");
}
// 重新获取焦点(能够和用户交互)
- (void)applicationDidBecomeActive:(UIApplication *)application
{
NSLog(@"BecomeActive");
}
// 应用程序进入后台的时候调用
// 一般在该方法中保存应用程序的数据, 以及状态
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"Background");
}
// 应用程序即将进入前台的时候调用
// 一般在该方法中恢复应用程序的数据,以及状态
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSLog(@"Foreground");
}
// 应用程序即将被销毁的时候会调用该方法
// 注意:如果应用程序处于挂起状态的时候无法调用该方法
- (void)applicationWillTerminate:(UIApplication *)application
{
}
// 应用程序接收到内存警告的时候就会调用
// 一般在该方法中释放掉不需要的内存
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
NSLog(@"MemoryWarning");
}
@end
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
//*******************************************************************************
//在上一个Demo中我们学习了 如何创建ViewController
//这里我们学习下如何添加
// UIView UIImageView UILabel
//*******************************************************************************
//首先我们先分别添加 UIView UIImageView UILabel 3个控件到self.view 上
//*******************************************************************************
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 100)];
view.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:0.6];
[self.view addSubview:view];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 120, 200, 100)];
imageView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.6];
[self.view addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 230, 200, 100)];
label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:0.6];
[self.view addSubview:label];
//编译运行。我们会看到3个控件除了我们分别设置的背景色 r g b 不同其余都一样,
//查看他们的头文件可见,他们都是UIView的子类,本质上就是UIView,不过是根据自己的需求定制化了不同的功能而已
//*******************************************************************************
//下面我们分别设置 imageView、 label 的各自属性 来看看他们的不同, 48 49行去掉注释
//*******************************************************************************
//imageView.image = [UIImage imageNamed:@"mao.jpg"];
//label.text = @"这是一只猫。";
//编译运行。 到这里我们可以看到他们各自独特的属性,一个可以设置图片属性,一个可以设置文字属性。
//以下是UILabel的一些其他属性,你可以试着修改,并观察效果。
// label.font = [UIFont systemFontOfSize:60];
// label.textColor = [UIColor whiteColor];
// label.shadowColor = [UIColor yellowColor];
// label.shadowOffset = CGSizeMake(1, 1);
// label.textAlignment = NSTextAlignmentCenter;
// label.lineBreakMode = NSLineBreakByCharWrapping;
// label.numberOfLines = 2;
//*******************************************************************************
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果图