您的位置:首页 > 产品设计 > UI/UE

纯代码创建UI界面入门(一)

2015-10-13 20:30 537 查看
一、删除main.storyboard,并在info.plist中删除main storyboard file base name

二、在-(BOOL)application:didFinishLauchingWithOptions:中添加以下代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//创建UIWindow对象
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//设置UIWindow的背景色
self.window.backgroundColor = [UIColor whiteColor];

//将该UIWindow对象设为主窗口并显示出来
[self.window makeKeyAndVisible];

//创建一个UIViewController对象
UIViewController *controller = [[UIViewController alloc] init];
self.window.rootViewController = controller;

//创建一个UIView对象
UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//设置controller显示rootView
controller.view = rootView;

//创建一个按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(120, 100, 80, 40);
[button setTitle:@"确定" forState:UIControlStateNormal];
[button setTitle:@"取消" forState:UIControlStateHighlighted];

[rootView addSubview:button];

//创建一个UILabel对象
self.show = [[UILabel alloc] initWithFrame:CGRectMake(60, 40, 180, 30)];
[self.show setText:@"初始文本"];
[self.show setBackgroundColor:[UIColor redColor]];
[rootView addSubview:self.show];

//为圆角按钮的触碰事件绑定事件处理方法
[button addTarget:self action:@selector(clickHandler:) forControlEvents:UIControlEventTouchUpInside];

[self.window addSubview:rootView];

// Override point for customization after application launch.
return YES;
}


- (IBAction)clickHandler:(id)sender
{
self.show.text = @"hahah";
}


注:1、由于学习iOS的时候已经是iOS7,所以外国的教程中都推荐用IB来写界面。不过为了更好地理解界面的底层实现,还是要学习以下纯代码编写。

2、因为之前有cocos2d-x的学习经验,所以设置对象,添加子视图的操作还是比较熟悉的。

参考资料:《疯狂ios讲义》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ui 代码 界面 iOS