iOS学习笔记2-UIView的嵌套

          前面我们大致了解了UIView的情况,当然,日常的开发中,UIView还需要更多的功能,其中最多的,就是UIView的嵌套,下面就来了解一下:

          利用addSubView方法在UIView追加子元素,这里我们依然用前面的代码进行添加:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // label
    self.window.backgroundColor = [UIColor blueColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
    label.frame = CGRectMake(0, 0, 200, 50);
    label.center = CGPointMake(160, 240);
    label.text =@"UIView";
    label.textColor = [UIColor redColor];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    [self.window addSubview:label];
    //加按钮

    //这里就按钮的基本设置做了简要介绍,以后有机会,会就UIButton的更多用法做详细介绍

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(120, 50, 80, 60);
    button.backgroundColor = [UIColor whiteColor];
    [button setTitle:@"butto" forState:UIControlStateNormal];
    [self.window addSubview:button];
    
    //加图片

    //这里注意,我一开始老是以为初始化了UIImage,就能直接加到主界面上,其实完全错了,一定要先初始化UIImage,在初始化UIImageView作为image的载体,加载到主界面

    UIImage *image = [UIImage imageNamed:@"1"];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.frame = CGRectMake(100, 150, image.size.width, image.size.height);
    [self.window addSubview:imageView];
    
    [self.window makeKeyAndVisible];
    return YES;
}

效果图如下:

今天就到这儿,晚安!


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值