ios UI注册登录界面

这一个知识章节已经结束了,你会发现这扇大门深处的世界是多么的富有挑战性,是不是很期待?当然了,我们现在还在新手村,不能去打副本什么的。那么今天我代理大家去玩玩。你们准备好了吗?偷笑。这次任务比较多。

好啦,我们准备好我们的装备武器--新建一个工程:


#import "AppDelegate.h"


@interface AppDelegate ()<UITextFieldDelegate>


@end


@implementation AppDelegate



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


    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];

    

    self.window.backgroundColor = [UIColor whiteColor];

    //调用下面的方法

    [self listenKeyboardNot];

    [self createControl];

    [self createTextFields];

    [self createButtons];

    [self createLabels];

     [self.window makeKeyAndVisible];

    

    return YES;

}

#pragma MARK - 监听键盘通知

-(void)listenKeyboardNot

{

   //这个是监听通知中心,就像放了个监听器,我们知道它会做什么,做了什么,这样我们就可以做点事情了。

    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];

  //当我们监听到消息的时候,就启动下面这两个方法,然后让他们做事情。

   //当键盘隐藏的时候

    [center addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

   //当键盘开启的时候

    [center addObserver:self selector:@selector(KeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}

//键盘开启的时候,就把y轴的坐标往上移

-(void)KeyboardWillShow:(NSNotification *)not

{

    for (int i = 0; i<2; i++) {

        UIButton *btn = (id)[self.window viewWithTag:100+i];

        btn.frame = CGRectMake(70+i*150, 490-100, 80, 40);

    }

    

}

//键盘隐藏的时候就恢复坐标位置

-(void)KeyboardWillHide:(NSNotification *)not

{

    for (int i = 0; i<2; i++) {

        UIButton *btn = (id)[self.window viewWithTag:100+i];

        btn.frame = CGRectMake(70+i*150, 490, 80, 40);

    }

}

#pragma mark - 加个模版好看点喽

-(void)createControl

{

   //创建一个用户界面控制器,他的边界是bounds

    UIControl *ctl = [[UIControl alloc]initWithFrame:self.window.bounds];

    ctl.backgroundColor = [UIColor brownColor];

   //设置透明度为0.3,我喜欢透明一点地。(alpha 

    ctl.alpha = 0.3;

    //这个是调用下面地ctlClick方法(下面那个ctlClick这个方法是用来监听用户点击事件的具体下面说,继续看)

    [ctl addTarget:self action:@selector(ctlClick) forControlEvents:UIControlEventTouchDown];

    [self.window addSubview:ctl];

  

    

    //ctl控件挪到窗口所有子视图的后面--之前在视图层次结构那漏讲了,这里补充下

    [self.window sendSubviewToBack:ctl];

}


#pragma mark - 创建文本输入框

-(void)createTextFields

{

   //这个是创建用户名的文本框

    UITextField *account = [[UITextField alloc]init];

    account.frame = CGRectMake(120, 120, 200, 40);

    account.borderStyle = UITextBorderStyleRoundedRect;

    account.font = [UIFont systemFontOfSize:20];

    account.keyboardType = UIKeyboardTypeNumberPad;

    account.delegate = self;

    [self.window addSubview:account];

    

    


   //这个是创建密码文本框    

    UITextField *pwd = [[UITextField alloc]init];

    pwd.frame = CGRectMake(120, 240, 200, 40);

    pwd.borderStyle = UITextBorderStyleRoundedRect;

    pwd.font = [UIFont systemFontOfSize:20];

    pwd.clearButtonMode = UITextFieldViewModeAlways;

    

    pwd.secureTextEntry = YES;

 //这个方法一开始你一定很困惑,这个有什么用,这里就不告诉你了,你自己调试下程序,你就能发现其中的秘密

    pwd.clearsOnBeginEditing =YES;

    //调用代理协议方法--这个还一时不好说,等到后面我会抽一节来细细讲,现在就先用着(对了最上面的不要忘记添加协议方法就是那个<  >里面的)

    pwd.delegate = self;

    [self.window addSubview:pwd];

    

    

}

#pragma mark - 创建注册登录

-(void)createButtons

{

//这里是用数组的方法,你学过oc就知道了,数组

    NSArray *array = @[@"注册",@"登录"];

    for (int i = 0; i<array.count; i++) {

        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

        btn.frame = CGRectMake(70 + i *100, 550, 155, 40);

        [btn setTitle:array[i] forState:UIControlStateNormal];

        btn.titleLabel.font = [UIFont systemFontOfSize:20];

        btn.tag = 100+i;

        [self.window addSubview:btn];

    }

}


-(void)createLabels

{

    NSArray *array = @[@"用户名:",@"密码:"];

    for (int i = 0; i<array.count; i++) {

        UILabel *label = [[UILabel alloc]init];

        label.frame = CGRectMake(50, 120+i*120, 65, 40);

        

        label.text = array[i];

        label.font = [UIFont systemFontOfSize:19];

        [self.window addSubview:label];

    }

}

//这里就是在上面调用的方法了,监听用户点击事件,当用户点击屏幕的时候,就会把键盘回收,我用的是最简单的方法,还有2个同样功能的方法我这里就不写了,这样是取消成为第一响应者方式的一种,如果返回yes,textfield自动成为第一响应者,当textfield进入编辑模式,并且弹出键盘。这是在UITextField里面的方法

-(void)ctlClick

{

    [self.window endEditing:YES];

}

@end









  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值