1. 和之前选用的是命令行,现在要使用Simple View Application框架来学校UI界面设计,如下图:
创建成功后,自动生成了一些文件,如下图:
2. main.m文件是入口文件,一般不需要改动,只需要改动相应的AppDelegate文件即可。
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
3. Appdelegate文件描述的是应用程序的生命周期,内容如下:
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
//应用程序启动后,调用此方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[ViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
//应用程序将要失活的时候,调用此方法
- (void)applicationWillResignActive:(UIApplication *)application {
}
//应用程序进入后台之后,调用此方法
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
//应用程序将要回到前台的时候,调用此方法
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
//应用程序激活后,调用此方法
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
//应用程序将要中止的时候,调用此方法---时间一般为5分钟,最多10分钟
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end
注:该代理文件主要应用的方法是application:didFinishLaunchingWithOptions:,该方法加载ViewController到窗口上。例:
//应用程序启动后,调用此方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];//初始化窗口
self.window.rootViewController = [[ViewController alloc] init];//加载ViewController到window上
[self.window makeKeyAndVisible];//使窗口可见
return YES;
}
然后在ViewController画图后运行就可以显示界面了,运行效果图如下:
ViewController文件内容如下:
#import "ViewController.h"
#define VIEWTAG 20 //一般情况tag不要设置10以内的,因为在系统使用
#define LONGSTR @"后来发现IOS7的视图有个边缘延伸的属性:edgesForExtendedLayout,其默认值是U"
@interface ViewController (){
UILabel *_label;
UITextField *_textField;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//状态栏:20pt,导航栏:44pt
//设置背景颜色,必须要设置,不然的话有延迟的效果,不流畅
//尺寸字体大小17:系统默认字体大小,黑色:系统默认背景颜色
self.view.backgroundColor = [UIColor whiteColor];
//添加belowView子视图
UIView *belowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
belowView.backgroundColor = [UIColor blueColor];
belowView.tag = VIEWTAG;
belowView.alpha = 0.5;
[self.view addSubview:belowView];//添加子视图到父视图
//添加view子视图
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
//设置视图的属性
view.backgroundColor = [UIColor redColor];
// view.hidden = YES;//是否隐藏
view.tag = VIEWTAG + 1;
[self.view addSubview:view];
// [view removeFromSuperview];//将view视图从父视图移除
[self.view bringSubviewToFront:belowView];//将belowView视图带到最上边
//layer层的设置
view.layer.cornerRadius = 50.0f;//边角半径
view.layer.shadowColor = [UIColor blackColor].CGColor;//设置阴影颜色
view.layer.shadowOffset = CGSizeMake(10, 10);//设置阴影偏离
view.layer.shadowOpacity = 1;//0-1,(默认是0:不可见)
//UIButton
//UIButtonTypeSystem系统自带的,有些设置不起作用,它用系统设置的(一般都是使用UIButtonTypeCustom,不过下边cancel宽度就不会自动变化)
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.center = self.view.center;//将button的中心和self.view的中心对上
button.bounds = CGRectMake(0, 0, 50, 40);
[button setTintColor:[UIColor blackColor]];//边框颜色
[button setTitle:@"OK" forState:UIControlStateNormal];//非选中状态的字体为'OK'
[button setTitle:@"Cancel" forState:UIControlStateSelected];//选中状态的字体为‘Cancel’
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
//UILabel
_label = [[UILabel alloc] initWithFrame:CGRectMake(100, 400, 200, 0)];
_label.numberOfLines = 0;//行数,0为任意
_label.lineBreakMode = NSLineBreakByWordWrapping;
_label.text = LONGSTR;
_label.font = [UIFont systemFontOfSize:17.0];
[self.view addSubview:_label];
[_label sizeToFit];//设置label的bounds随着字体的高度自动适应大小
//设置背景图片
UIImageView *headerImageView = [[UIImageView alloc] init];
headerImageView.center = CGPointMake(CGRectGetMidX(self.view.frame), CGRectGetMaxY(self.view.frame)*0.8);
headerImageView.bounds = CGRectMake(0, 0, 100, 100);
headerImageView.layer.cornerRadius = 10;
headerImageView.layer.masksToBounds = YES;//图片本来是方形,上述设置也不可以,只有添加此行才有圆角效果
headerImageView.userInteractionEnabled = YES;//默认是NO;
headerImageView.image = [UIImage imageNamed:@"logo"];
[self.view addSubview:headerImageView];
}
//设置按钮点击时的动作
- (void)buttonPressed:(UIButton *)sender{
sender.selected = !sender.selected;//该句必须设置,否则点击‘OK’时不转换到’Cancel‘
}
@end