iOS培训-UI用户界面基础一界面组件创建

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














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值