【iOS】-------UI学习

UIView

 

UIView对象是一个视图对象,屏幕上能看到的对象都是UIView的子类,继承于UIView.

UIView* view = [[UIView alloc] init];
    view.frame = CGRectMake(100, 100, 100, 50);
    view.backgroundColor = [UIColor purpleColor];
    //将新建的视图添加到父类视图上
    //将新建视图显示到屏幕上
    //将视图作为父亲视图的子视图管理起来
    [self.view addSubview: view];
    //是否隐藏
    view.hidden = NO;
    //设置透明度
    //1不透明,0透明,0.5半透明
    view.alpha = 1;
    self.view.backgroundColor = [UIColor clearColor];
    //是否显示不透明
    view.opaque = NO;
    //将自己从父亲视图删除
    //[view removeFromSuperview];

通过addSubview:这个方法添加子类,不管谁添加它,只要越晚添加,视图就在越上层。
移除父视图也会把它得子视图移除。

UIView* view01 = [[UIView alloc] init];
    view01.frame = CGRectMake(100, 100, 150, 150);
    view01.backgroundColor = [UIColor blueColor];
    UIView* view02 = [[UIView alloc] init];
    view02.frame = CGRectMake(125, 125, 150, 150);
    view02.backgroundColor = [UIColor purpleColor];
    UIView* view03 = [[UIView alloc] init];
    view03.frame = CGRectMake(150, 150, 150, 150);
    view03.backgroundColor = [UIColor greenColor];
    //按添加顺序绘制,还会覆盖
    [self.view addSubview:view01];
    [self.view addSubview:view02];
    [self.view addSubview:view03];
    //将某个视图调到最前面
    [self.view bringSubviewToFront:view01];
    //将某个视图调整到最后面
    [self.view sendSubviewToBack:view03];
    //subView:管理所有self.View的子视图的数组
    //可以通过它找到子视图
    //最前面的视图下表为0;
    UIView* viewFront = self.view.subviews[2];
    UIView* viewBack = self.view.subviews[0];
    if (viewFront == view01) {
        NSLog(@"相等");
    }
    [view01 removeFromSuperview];

UIButton 

注意button只能通过类方法创建,不能使用alloc

分为文字按钮和图像按钮

//只能通过类方法创建buttonWithType
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 100, 40);
    [btn setTitle:@"按钮1" forState:UIControlStateNormal];
    [btn setTitle:@"按下按钮" forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor purpleColor] forState:UIControlStateHighlighted];
    //设置图片颜色。不分状态,优先级没有setTitleColor高
    [btn setTintColor:[UIColor whiteColor]];
    //titleLabel实际是UILable对象
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.view addSubview:btn];
}
- (void) creatImageButton {
    //创建自定义类型btn
    UIButton* btnImage  = [UIButton buttonWithType:UIButtonTypeCustom];
    btnImage.frame = CGRectMake(100, 200, 50, 50);
    //两张图片加载
    UIImage* icon01 = [UIImage imageNamed:@"WechatIMG99.jpeg"];
    UIImage* icon02 = [UIImage imageNamed:@"WechatIMG100.jpeg"];
    //设置按钮图片
    [btnImage setImage:icon01 forState:UIControlStateNormal];
    [btnImage setImage:icon02 forState:UIControlStateHighlighted];
    [self.view addSubview:btnImage];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatRectButton];
    [self creatImageButton];
}

UI事件处理:添加事件函数

UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 100, 80, 80);
    [btn setTitle:@"按钮1" forState:UIControlStateNormal];
    //这里的self代表ViewController对象
    //如果满足后面事件,则调用前面该对象函数
    [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(touchdown) forControlEvents:UIControlEventTouchDown];
    UIButton* btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn2 setTitle:@"按钮2" forState:UIControlStateNormal];
    btn2.frame = CGRectMake(200, 100, 80, 80);
    [self.view addSubview:btn2];
    //多个btn可以使用同一个事件函数
    [btn2 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 1;
    btn2.tag = 2;
    
}
/*- (void)pressBtn2 {
    NSLog(@"按钮2按下");
}*/
- (void)touchdown {
    NSLog(@"按钮被触碰");
}
- (void)pressBtn:(UIButton*) btn {
    if (btn.tag == 1) {
        NSLog(@"按钮1被按下");
    }
    if (btn.tag == 2) {
        NSLog(@"按钮2被按下");
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatBtn];
}

UIWindow


UIWindow是UIview的子类,所以UIWindow就是一个View,就是增加许多系统的能力,只不过使用的时候,一般把它当容器用。UIWindow有个属性是rootViewController来跟UIViewController相连,然后以他为附着体,来进行真正的显示。

 一个App中可以包含多个Window,但是只能有一个是主窗口keyWindow

App启动完成后,创建的第一个视图控件就是UIWindow, 接着创建rootViewController对应的view, 最后将该view 添加addSubview到UIWindow上, 于是根视图控制器的view就显示在屏幕上了 


 

 //创建一个UIWindow对象
        //整个程序中只有一个UIWindow对象
        //在程序基本上表示屏幕窗口
        //UIWindow继承于UIView
        //UIWindow是一个特殊的UIView
        //UIScreen:表示屏幕硬件表示类
        //mainScreen获得主屏幕设备信息
        //bounds表示屏幕的宽高值
        self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        
        //创建一个视图控制器做为UIWindow的跟视图控制器
        self.window.rootViewController = [[UIViewController alloc] init];
        
        //设置背景颜色
        self.window.backgroundColor = [UIColor blueColor];
        
        UIView* view = [[UIView alloc] init];
        view.frame = CGRectMake(100, 100, 150, 150);
        view.backgroundColor = [UIColor orangeColor];
        //背景视图
        UIView* backView = [[UIView alloc] init];
        backView.frame = CGRectMake(50, 50, 240, 360);
        backView.backgroundColor = [UIColor greenColor];
        
        //将backView做为view的父亲视图
        //子视图的坐标是参照父亲视图的坐标系移动
        //父视图坐标移动,所有的子视图都会移动
        [backView addSubview: view];
        
        [self.window addSubview: view];
        
        //每一个view都有一个window属性
        //view.window;
        //backView.window;
        
        
        //使window有效并显示到屏幕上
        [self.window makeKeyAndVisible];
        
        NSLog(@"%@",view.window);
        NSLog(@"%@",backView.window);
        NSLog(@"%@",self.window);
        //self.window.rootViewController.window
        //它们是一个window

UILabel

UILabel是可以显示在屏幕上,并且可以显示文字的一种UI视图
UILabel对象需要在ViewController.m文件中创建

- (void) creatUI {
    UILabel* label = [[UILabel alloc] init];
    //对象为字符串
    label.text = @"fuchung";
    label.frame = CGRectMake(100, 100, 160, 40);
    label.textColor = [UIColor purpleColor];
    label.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:label];
    //默认字体大小为12
    label.font = [UIFont systemFontOfSize:20];
    label.shadowColor = [UIColor blackColor];
    label.shadowOffset = CGSizeMake(5, 5);
    //文字的对齐
    label.textAlignment = NSTextAlignmentLeft;
    label.numberOfLines = 0;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

登录界面案例 

包括UILabel和UITextfield和警告对话框

@interface ViewController : UIViewController
{
    //用户名提示Label
    UILabel* _lbUserName;
    //密码提示Label
    UILabel* _lbPassword;
    //用户名输入框
    UITextField* _tfUserName;
    //密码输入框
    UITextField* _tfPassword;
    //登录时按钮
    UIButton* _btLogin;
    //注册按钮
    UIButton* _btRegister;
}

@end
_lbUserName = [[UILabel alloc] init];
    _lbUserName.frame = CGRectMake(20, 60, 80, 40);
    _lbUserName.text = @"用户名";
    _lbUserName.font = [UIFont systemFontOfSize:20];
    _lbUserName.textAlignment = NSTextAlignmentLeft;
    
    _lbPassword = [[UILabel alloc] init];
    _lbPassword.frame = CGRectMake(20, 140, 80, 40);
    _lbPassword.text = @"密码";
    _lbPassword.font = [UIFont systemFontOfSize:20];
    _lbPassword.textAlignment = NSTextAlignmentLeft;
    
    _tfUserName = [[UITextField alloc] init];
    _tfUserName.frame = CGRectMake(120, 60, 180, 40);
    _tfUserName.placeholder = @"请输入用户名";
    _tfUserName.keyboardType = UIKeyboardTypePhonePad;
    _tfUserName.borderStyle = UITextBorderStyleRoundedRect;
    
    _tfPassword = [[UITextField alloc] init];
    _tfPassword.frame = CGRectMake(120, 140, 180, 40);
    _tfPassword.placeholder = @"请输入密码";
    _tfPassword.keyboardType = UIKeyboardTypeDefault;
    _tfPassword.borderStyle = UITextBorderStyleRoundedRect;
    
    _btLogin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btLogin.frame =CGRectMake(120, 300, 80, 40);
    [_btLogin setTitle:@"登录" forState:UIControlStateNormal];
    [_btLogin addTarget:self action:@selector(pressLogin) forControlEvents:UIControlEventTouchUpInside];
    
    _btRegister = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    _btRegister.frame =CGRectMake(120, 360, 80, 40);
    [_btRegister setTitle:@"注册" forState:UIControlStateNormal];
    [_btRegister addTarget:self action:@selector(pressRegister) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_lbUserName];
    [self.view addSubview:_lbPassword];
    [self.view addSubview:_tfUserName];
    [self.view addSubview:_tfPassword];
    [self.view addSubview:_btLogin];
    [self.view addSubview:_btRegister];
    
}
- (void)pressLogin {
    NSString* name = @"fuchuang";
    NSString* password = @"fu135453";
    NSString* strTextName = _tfUserName.text;
    NSString* strTextpass = _tfPassword.text;
    if ([name isEqualToString:strTextName] && [password isEqualToString:strTextpass]) {
        NSLog(@"用户名正确");
        UIAlertController* alView = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证成功, 登录成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
        [alView addAction:action1];
        [self presentViewController:alView animated:YES completion:nil];
    } else {
        UIAlertController* alView = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证失败, 用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action1 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {}];
        [alView addAction:action1];
        [self presentViewController:alView animated:YES completion:nil];    }
}
- (void)pressRegister {
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [_tfUserName resignFirstResponder];
    [_tfPassword resignFirstResponder];
}

       

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值