23.UIView

UIView

介绍

  • UIView是组成画面的基本元素,拥有位置属性、一定尺寸的四边形,一般有背景色。
  • UIView可以拥有UIView类型的子元素(追加UIView)
  • 在iphone应用程序中,应用程序的UIWindow也是UIView的子类

位置属性

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

    //frame设置(设置X,Y,高
    label.frame = CGRectMake(x, y, w, h);

    //获取frame的各种值
    NSLog(@"x = %f, label.frame.origin.x");
    NSLog(@"x = %f, label.frame.origin.y");
    NSLog(@"x = %f, label.frame.size.width");
    NSLog(@"x = %f, label.frame.size.hight");

    //center设置(以label为中心设置X和Y)
    label.center = CGPointMake(X, Y);

    //获取center的各种值
    NSLog(@"x = %f, label.center.x");
    NSLog(@"x = %f, label.center.y");

显示设置

    //隐藏控件(但控件是隐藏的,显示出来。否则相反)
    label.hidden = !label.hidden;

    //颜色设置
    //(1)方法一
    label.backgroundColor = [UIColor redColor];

    //(2)方法二(自定义颜色,RGB和透明度(0<=1))
    label.backgroundColor = [[UIColor alloc] initWithRed:1.0 green:0 blue:0 alpha:0];

    //透明度设置(0<=1)
    label.alpha = 1.0;    //控件透明度
    label.backgroundColor = [label.backgroundColor colorWithAlphaComponent:1.0];   //设置控件背景颜色透明度

    //如果view1 < view2(尺寸大小),隐藏超出view1的view2部分设置
    view1.clipsTobounds = YES;

    //拉申图片(0,0, 1.0,1.0)为原图效果
    imageV.layer.contentsRect = CGRectMake(0, 0, 1.0, 0.5);

    //根据控件内容自动调整大小
    [label sizeToFit];

显示位置

    //Sanle的会改变图片的尺寸(不会改变比例),其他的只会移动位置
    //UIViewContentModeCenter          中心
    //UIViewContentModeTop             中上
    //UIViewContentModeBottom          中下
    //UIViewContentModeLeft            中左
    //UIViewContentModeRight           中右
    //UIViewContentModeTopLeft         左上
    //UIViewContentModeTopRight        右上
    //UIViewContentModeBottomLeft      左下
    //UIViewContentModeBottomRight     右下
    //UIViewContentModeScaleToFill     填充整个View
    //UIViewContentModeScaleAspectFit  不超出view高/宽度,伸缩至最大尺寸(不改变图片比例)
    //UIViewContentModeScaleAspectFill 超出view高/宽度,伸缩至最大尺寸(不改变图片比例)
    imageView.contentMode = UIViewContentModeScaleAspectFit;

    //以图片为中心点设置x和y
    imageView.center = CGPointMake(x , y);

    //旋转90°角
    imageV.transform = CGAffineTransformMakeRotation(90 * (M_PI / 180.0));

    //扩大/缩放(宽度3.0倍、高度1.0倍)
    imageV.transform = CGAffineTransformMakeScale(3.0, 1.0);

    //反转(参数1位:CGAffineTransform 参数2为左右,参数3位上下)
    imageV.transform = CGAffineTransformScale(imageV.transform, -1.0, -1.0);

    //平移(若在反转等后面,会直接让他们无效)
    imageV.transform = CGAffineTransformMakeTranslation(X, Y);

追加控件

    //addSubView可以用于View/button/label等等
    [self.view addSunbView: 控件];
    [button addSunbView: 控件];

    //insertSubview可以插入控件,同时设置标签
    [self.view insertSubview: 控件 atIndex: NSInteger];

    //在控件2 上 插入控件1
    [self.view insertSubview: 控件1 aboveSubview: 控件2];

    //在控件2 下 插入控件1
    [self.view insertSubview: 控件1 belowSubview: 控件2];

    //标签1与标签2的控件交换顺序
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:2];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值