UI UIWindow / bounds / frame

、、、、、、、、、、、、、AppDelegate.m 、、、、、、、、、、、、、、、、、


#import "AppDelegate.h"

#define containerFindPassword 100
#define containerRegiste 101

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
#pragma mark  ========= 1、 创建主窗口 ==================================
   // UIWindow : UIView
   // ===>1、创建窗口跟创建对象一样:申请空间-》初始化
    self.window = [[UIWindow alloc]init];
   //====》2、赋值(窗口肯定是有大小的嘛,跟当前屏幕一样大)
    self.window.frame = [UIScreen mainScreen].bounds;
   //====>3、跟我们平时装修一样,得为这个窗口挑选一个颜色吧。
    self.window.backgroundColor = [UIColor whiteColor];
   //=====》4、把窗口创建好了,得选择一个地方安置它吧,要不就成孤家寡人了,岂不很可怜
    [self.window makeKeyWindow];//设置成主窗口并显示
    
#pragma mark ========= 2、 为主窗口添加 子视图 ===========================
    
   // UIView 是UIWindow的父类 所以创建视图 跟创建窗口也是一样的。
    //把舞台布置好了之后吧,这个时候,得有演员上台表演吧,当然这个时候还没有涉及到表演,只是一个排练中的走位而已

    
    //1、创建视图对象
    //2、设置这个对象的位置吧,你想让这个演员站哪就站哪,目前你就相当于那个导演的功能,所以你现实生活中的导演梦没有完成,在UI在完全帮助你实现梦想

    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 100, 100)];
    //3、也得跟这个对象设置一种颜色吧,就像上妆一样,打扮的漂漂亮亮的
    redView.backgroundColor = [UIColor redColor];
    //4、把试图对象创建好了之后,总得派上用场吧,终于可以站上舞台了,想想还有点小激动呢
    [self.window addSubview:redView];
    
    
#pragma mark  ============ 3、 再接着添加一个子视图    =====================
    
    UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(100, 120, 80, 120)];
    yellowView.backgroundColor = [UIColor yellowColor];
    //是根据父视图的坐标系制定自己的位置
//    [self.window addSubview:yellowView];
    [redView addSubview:yellowView];
    
#pragma mark =======  4、移除window中的所有子视图 =======================
    //移除方法中,接收消息的是要移除的视图本身,也就是告诉自己 要把自己从父视图中移除出来
    [redView removeFromSuperview];
    [yellowView removeFromSuperview];
    
#pragma mark ========= 5、不同父视图的层次感 ==============
    
    UIView *blackView=[[UIView alloc]initWithFrame:CGRectMake(100, 140, 30, 30)];
    blackView.backgroundColor = [UIColor blackColor];
    
    UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(90, 60, 20, 20)];
    greenView.backgroundColor = [UIColor greenColor];
    
    UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
    blueView.backgroundColor = [UIColor blueColor];
    //父视图 addSubView:子视图
    //一层接一层

    [self.window addSubview:blackView];
    [blackView addSubview:greenView];
    [greenView addSubview:blueView];
    //注意的是,每一层的父视图是谁
    
    //移除掉
    [blackView removeFromSuperview];
    [greenView removeFromSuperview];
    [blueView removeFromSuperview];
 
#pragma mark ========== 6、tag 的使用 =============================
    
    UIView *grayView = [[UIView alloc]initWithFrame:CGRectMake(120, 120, 100, 230)];
    
    grayView.backgroundColor = [UIColor grayColor];
    
    [self.window addSubview:grayView];
    //tag 默认是0.所以不要设置为0,因为所有的对象都是0,设置为0的话,太多,就娶不到对象
    grayView.tag = 1;
    
    [self test];
    
    [grayView removeFromSuperview];
    
#pragma mark ============ 7、同一个父视图的层次感 ======
    
    UIView *firstView = [[UIView alloc]init];
    firstView.frame = CGRectMake(0, 0, 50, 90) ;
    firstView.backgroundColor = [UIColor redColor];
    
    UIView *secondView = [[UIView alloc]init];
    secondView.frame = CGRectMake(0, 0, 200,30);
    secondView.backgroundColor = [UIColor blackColor];
    
    [self.window insertSubview:firstView atIndex:1];
    [self.window insertSubview:secondView atIndex:10];
    //用addSubViews 如果两张视图不是覆盖关系的话,视图显示就是在一个平面上,why?
    
    
    //其实addSubView都是在一个层面上的,就以栈的方式 是一个接一个放上去的,可以把他当做数组解决,取下标的就能得到值。
    
    
    
    //练习这个的时候,简直就是有点晕菜了,本来上课的时候,就发现层次感来着,可是自己改了好几次都是在同一个平面上的,当我已经在上面写好就是一个平面Model的时候,再运行一次发现,层次感又出来了,这不是打自己脸么,我也是够了,也不知道自己先前是怎么回事,哎,总之,代码这个东西还是要反复斟酌才是,不然,很多小细节,自己都不知道是怎么回事的,以后做项目的时候,才会考虑到更多

    [firstView removeFromSuperview];
    [secondView removeFromSuperview];
#pragma mark ============ 8、获取自己的子视图和父视图   ============
    UIView *oneView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    oneView.backgroundColor= [UIColor redColor];
    
    UIView *twoView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    
    twoView.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:oneView];
    [self.window addSubview:twoView];
//    [self.window insertSubview:oneView atIndex:3];
//    [self.window insertSubview:twoView atIndex:4];
    
    NSLog(@"self.window的子视图:%@",[self.window subviews]); //NSArray
    
    NSLog(@"oneView的父视图:%@",[oneView superview]); //NSInteger
    
    [oneView removeFromSuperview];
    [twoView removeFromSuperview];
#pragma mark =========  9、bounds&frame区别 ============================
    //======1、bounds
    UIView *pointView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 120, 80)];
    pointView.backgroundColor = [UIColor redColor];
//    //改变window的坐标系原点是(50,70) 以及 尺寸(大小)
//    self.window.bounds=CGRectMake(50, 70, 150, 250);
//    //当父视图的坐标系改变之后,要注意子视图相对于 新坐标的位置。
//    //以后做项目的时候,要仔细分析清楚各个视图的相对位置

//    [self.window addSubview:pointView];
    
    //========2、frame
    //改变了window的位置和大小,但是坐标系原点还是(0,0),这个(0,0)是(50,70)这个的点的位置
    self.window.frame = CGRectMake(50, 70, 250, 250);
    [self.window addSubview:pointView];
    
    //所以,要想改变自己的位置有两种方式:
    //====== 1 ========= 改变自己的frame值。
    //====== 2 ========= 改变父类的bounds值

    
    [pointView removeFromSuperview];
    self.window.frame = [[UIScreen mainScreen] bounds];
    
#pragma mark ============= 10、center ===================================
    //是以父视图为基准
    UIView *centerView = [[UIView alloc]init];
    
    centerView.frame = CGRectMake(0, 0, 100, 100);
    
    centerView.backgroundColor = [UIColor redColor];
    //修改frame,会影响center的坐标。==》frame 是以父视图为坐标系
//    self.window.frame = CGRectMake(0, 0, 100, 100);
    //修改bounds,并不会影响center的坐标。==》bounds 是以自身为坐标系
    self.window.bounds = CGRectMake(0, 0, 100, 100);
    
    centerView.center = self.window.center;
    
    [self.window addSubview:centerView];
    
    NSLog(@"centerView中心点:%.f,%.f,self.window中心点:%.f,%.f",centerView.center.x,centerView.center.y,self.window.center.x,self.window.center.y);
    [centerView removeFromSuperview];
    self.window.frame = [[UIScreen mainScreen] bounds];
    
#pragma mark ========= 10、练习 ================================
    
    //新建一个工程,创建一个与window瞪大的视图containerView,然后用不同颜色的视图拼出一个注册界面。
    UIView *containerView = [[UIView alloc]init];
    containerView.frame = self.window.bounds;
    [self.window addSubview:containerView];
    //用UILable替换UIView。
    
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.frame = CGRectMake(50, 50, 100, 20);
    nameLabel.text = @"用户名:";
    [containerView addSubview:nameLabel];
    
    UITextField *nameTextField=[[UITextField alloc]init];
    nameTextField.frame = CGRectMake(130, 50, 200, 30);
    //占位符
    nameTextField.placeholder = @"手机号/邮箱";
//    nameTextField.text = @"手机号/邮箱";
//    //清除提示语
//    nameTextField.clearsOnBeginEditing = YES;
    //设置提示语的颜色
    nameTextField.textColor = [UIColor grayColor];
    //设置边框风格
    nameTextField.borderStyle = UITextBorderStyleRoundedRect;
    nameTextField.keyboardType = UIKeyboardTypeNumberPad;
    //是否允许输入
    nameTextField.enabled = YES;
    [containerView addSubview:nameTextField];
    nameTextField.tag=1;
    
    UILabel *passLabel = [[UILabel alloc]init];
    passLabel.frame = CGRectMake(50, 100, 50, 20);
    passLabel.text = @"密码:";
    [containerView addSubview:passLabel];
    
    UITextField *passTextField = [[UITextField alloc]init];
    passTextField.frame = CGRectMake(130, 100, 200, 30);
    //安全性
    passTextField.secureTextEntry = YES;
    passTextField.borderStyle = UITextBorderStyleRoundedRect;
    [containerView addSubview:passTextField];
    //再次点击时清除
    passTextField.clearsOnBeginEditing = YES;
    passTextField.tag=2;
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    loginButton.frame = CGRectMake(70, 150, 50, 20);
    //设置按钮标题
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    //设置标题颜色
    [loginButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    //@selector(执行的方法)
    [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:loginButton];
    
    UIButton *findPassButton = [[UIButton alloc]init];
    findPassButton.frame = CGRectMake(150, 150, 90, 20);
    [findPassButton setTitle:@"找回密码" forState:UIControlStateNormal];
    [findPassButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    //设置按键的响应事件
    [findPassButton addTarget:self action:@selector(findPassword) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:findPassButton];
    
    UIButton *registButton = [[UIButton alloc]init];
    registButton.frame = CGRectMake(270, 150, 50, 20);
    [registButton setTitle:@"注册" forState:UIControlStateNormal];
    [registButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    //设置按键的响应事件
    [registButton addTarget:self action:@selector(regist) forControlEvents:UIControlEventTouchUpInside];
    [containerView addSubview:registButton];
    
    
    return YES;
}
//3、实现协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
//登录按钮的事件响应反馈
-(void)login{
    //根据tag获取视图
    UITextField *nameInput =(UITextField *) [self.window viewWithTag:1];
    UITextField *passTextField = (UITextField *)[self.window viewWithTag:2];
    
    //得到文本编辑框的内容
    NSString *inputName = nameInput.text;
    NSString *password = passTextField.text;
    
    //设置代理对象
    AppDelegate *dele=[[AppDelegate alloc]init];
    NSString *title = @"提示";
    NSString *message;
    
    if ([inputName containsString:@".com"] && [password isEqualToString:@"123456"]) {
       message = @"登录成功";
        
    }else{
        message = @"用户名和密码不正确";
    }
    NSString *cancel = @"取消";
    NSString *other = @"确定";
    
    //提示框的反馈
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:title message:message delegate:dele cancelButtonTitle:cancel otherButtonTitles:other, nil];
    
    [alert show];
}
-(void)findPassword{
    //设置一个容器,用于装载另一个页面
    UIView *container = [[UIView alloc]init];
    container.frame = self.window.bounds;
    container.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:container];
    container.tag = containerFindPassword;
    
    UITextField *mailboxInput = [[UITextField alloc]init];
    mailboxInput.frame = CGRectMake(50, 230, 200, 20);
    mailboxInput.placeholder = @"电子邮箱";
    mailboxInput.borderStyle = UITextBorderStyleRoundedRect;
    [container addSubview:mailboxInput];
    
    UIButton *confirm = [[UIButton alloc]initWithFrame:CGRectMake(70, 270, 50, 20)];
    [confirm setTitle:@"找回" forState:UIControlStateNormal];
    [confirm setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [container addSubview:confirm];
    
    
    UIButton *cancel = [[UIButton alloc]initWithFrame:CGRectMake(130, 270, 50, 20)];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [container addSubview:cancel];
    //取消按钮的响应事件
    [cancel addTarget:self action:@selector(cancelFindPassword) forControlEvents:UIControlEventTouchDown];
    
}
//按钮反馈信息
-(void)cancelFindPassword
{
    [[self.window viewWithTag:containerFindPassword] removeFromSuperview];
    
}
//注册按钮的方法
-(void)regist{
    //定义一个容器视图
    UIView *container = [[UIView alloc]init];
    container.frame = self.window.bounds;
    container.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:container];
    container.tag = containerRegiste;
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 90, 20)];
    name.text = @"用户名";
    [container addSubview:name];
    
    UITextField *nameText = [[UITextField alloc]initWithFrame:CGRectMake(120, 100, 200, 20)];
    nameText.placeholder = @"请输入用户名";
    nameText.borderStyle = UITextBorderStyleRoundedRect;
    [container addSubview:nameText];
    
    UILabel *pass = [[UILabel alloc]initWithFrame:CGRectMake(50, 150, 90, 20)];
    pass.text = @"密码";
    [container addSubview:pass];
    
    UITextField *passText = [[UITextField alloc] initWithFrame:CGRectMake(120, 150, 200, 20)];
    passText.placeholder = @"请输入密码";
    passText.borderStyle = UITextBorderStyleRoundedRect;
    [container addSubview:passText];
    
    UILabel *confirmPass = [[UILabel alloc]initWithFrame:CGRectMake(50, 200, 90, 20)];
    confirmPass.text = @"确认密码";
    [container addSubview:confirmPass];
    
    UITextField *confirmPassText = [[UITextField alloc]initWithFrame:CGRectMake(120, 200, 200, 20)];
    confirmPassText.placeholder = @"再次输入密码";
    confirmPassText.borderStyle = UITextBorderStyleRoundedRect;
    [container addSubview:confirmPassText];
    
    
    UILabel *phone = [[UILabel alloc]initWithFrame:CGRectMake(50, 250, 90, 20)];
    phone.text = @"手机号";
    [container addSubview:phone];
    
    UITextField *phoneText = [[UITextField alloc]initWithFrame:CGRectMake(120, 250, 200, 20)];
    phoneText.placeholder = @"请输入联系人方式";
    phoneText.borderStyle = UITextBorderStyleRoundedRect;
    [container addSubview:phoneText];
    
    
    UILabel *mailbox = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 90, 20)];
    mailbox.text = @"邮箱";
    [container addSubview:mailbox];
    
    UITextField *mailboxText = [[UITextField alloc]initWithFrame:CGRectMake(120, 300, 200, 20)];
    mailboxText.placeholder = @"请输入邮箱";
    mailboxText.borderStyle = UITextBorderStyleRoundedRect;
    [container addSubview:mailboxText];
    
    UIButton *confirm = [[UIButton alloc]initWithFrame:CGRectMake(80, 350, 50, 20)];
    [confirm setTitle:@"注册" forState:UIControlStateNormal];
    [confirm setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [container addSubview:confirm];
    
    UIButton *cancel = [[UIButton alloc]initWithFrame:CGRectMake(150, 350, 50, 20)];
    [cancel setTitle:@"取消" forState:UIControlStateNormal];
    [cancel setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [container addSubview:cancel];
    //取消按钮的事件响应
    [cancel addTarget:self action:@selector(cancelRegiste) forControlEvents:UIControlEventTouchDown];
    
}
//取消按钮的反馈
-(void)cancelRegiste{
    [[self.window viewWithTag:containerRegiste] removeFromSuperview];
}

-(void)test{
    UIView *change = [self.window viewWithTag:1];
    change.backgroundColor = [UIColor redColor];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end



、、、、、、、、、 Label_TextView.m 、、、、、、、、、、、、、、、、、、、、


#import "Lablel_TextView.h"

@implementation Lablel_TextView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(id)init{
    self=[super init];
    if (self) {
        _label = [[UILabel alloc]init];
        _textField = [[UITextField alloc]init];
    }
    return self;
}

@end




、、、、、、、、、、 Label_TextView.h        、、、、、、、、、、、、、、

#import <UIKit/UIKit.h>

@interface Lablel_TextView : UIView

@property(nonatomic,retain) UILabel *label;
@property(nonatomic,retain) UITextField *textField;

-(id)init;

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值