黑马程序员--UI之UIview、UITextField、NSUserDefaults以及键盘的升起与回落的基本操作

------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

1.UIView的基本属性:

fram 坐标

center 中心点

bounds 边框大小

backgroundColor 背景色

tag 标签

contentMode 内容模式

SuperView 父视图

Subview 子视图

clipsToBounds 剪裁子视图

autoresizing 自动缩放

autoresizing 自动缩放

alpha 透明度(0-1)

接下来代码演示:

  UIView *v=[[UIView alloc]init];
    v.frame=CGRectMake(10, 30, 300, 40);
    v.backgroundColor=[UIColor redColor];
    [self.window addSubview:v];
    
    UIView *v1=[[UIView alloc]init];
    v1.frame=CGRectMake(0, 20, 200, 40);
    v1.backgroundColor=[UIColor greenColor];
    [v addSubview:v1];
    NSLog(@"fram - x=%f",v.frame.origin.x);      //打印视图的坐标以及大小,刚开始的时候对屏幕的把握不太好,可以多打印即便看看

    NSLog(@"fram - y=%f",v.frame.origin.y);
    NSLog(@"fram - width=%f",v.frame.size.width);
    NSLog(@"fram - height=%f",v.frame.size.height);
#pragma mark 中心点
    CGPoint point=v.center;              
    NSLog(@"center - x=%f",point.x);
    NSLog(@"center - x=%f",point.y);
    
    
#pragma mark 边框大小
   // CGRect bounds=v.bounds;//仅仅能得到宽和高,感觉这东西不如直接用fram

   
#pragma mark设置内容模式
    v.contentMode=UIViewContentModeScaleAspectFit;//****个人感觉这个还是很实用的
    
    UIView *v2=[[UIView alloc]init];
    v2.backgroundColor=[UIColor blackColor];
    v2.frame=CGRectMake(0, 20, 300, 40);
    [v1 addSubview:v2];
    
    
#pragma mark 得到父类试图
    UIView *superv1=v1.superview;
    superv1.backgroundColor=[UIColor grayColor];
#pragma mark 得到子类视图数组
    NSArray *subv=v.subviews;
    NSLog(@"%d",[subv count]);
#endif

2.接下来是UITextField和UITextview的基本使用,我用的是全局变量

{
    UITextField *_tf;
    UITextView *_tv;
    UISwitch *_sw;
    UIView *_vt;
    UISlider *_sli;
    UIButton *_btn;
}
    _vt=[[UIView alloc]init];
    _vt.frame=CGRectMake(10, 180, 300, 200);
   // _vt.backgroundColor=[UIColor blackColor];
    [self.window addSubview:_vt];
    
    _tv=[[UITextView alloc]init];
    _tv.frame=CGRectMake(10, 180, 300, 200);
    _tv.backgroundColor=[UIColor blackColor];
    _tv.text=@"wo shi yi zhi small bee";
    _tv.font=[UIFont boldSystemFontOfSize:30];
    _tv.textColor=[UIColor yellowColor];
    
    _tf=[[UITextField alloc]init];
    _tf.frame=CGRectMake(10, 440, 200, 30);
    //_tf.backgroundColor=[UIColor grayColor];
    _tf.borderStyle=UITextBorderStyleRoundedRect;
    [self.window addSubview:_tf];

3.重点:键盘的升起与回落,很实用!效果一级棒得意(例如微信你用微信输入内容的时候,输入框会随着键盘一起升起来,应该就是这么实现的吧)

#pragma mark 键盘升起
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardwillshow) name:UIKeyboardWillShowNotification object:nil];
#pragma mark  键盘降落
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardwillHide) name:UIKeyboardWillHideNotification object:nil];
- (void)keyboardwillHide{
    [UIView animateWithDuration:0.25 animations:^{_btn.frame=CGRectMake(200, 440, 60, 30);_tf.frame=CGRectMake(10, 440, 200, 30);} completion:^(BOOL finishde){}];
}
-(void)keyboardwillshow{
    [UIView animateWithDuration:0.25 animations:^{_tf.frame=CGRectMake(10, 100, 200, 30);_btn.frame=CGRectMake(200, 100, 60, 30);} completion:^(BOOL finishde){
    }];

4.记录程序是否是第一次运行,可以用此功能左导航界面

    NSUserDefaults *nsuser=[NSUserDefaults standardUserDefaults];

    if (![nsuser objectForKey:@"isfirst"]) {
        NSString *str=@"非第一次运行";
        [nsuser setObject:str forKey:@"isfirst"];
        [nsuser synchronize];
    }else{
        la.text=[nsuser objectForKey:@"isfirst"];
    }
#pragma mark 删除数据
//    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"isfist"];
//    [[NSUserDefaults standardUserDefaults]synchronize];
    
//    NSLog(@"fram - width=%f",self.window.frame.size.width);
//    NSLog(@"fram - height=%f",self.window.frame.size.height);







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值