ios- UIView 相关学习

子view与父view

子view布局

    // UIview 布局方式
    _backView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 120, 120)];
    _backView.backgroundColor = [UIColor blackColor];
    //允许子view布局方式
    _backView.autoresizesSubviews = YES;
    [self.window addSubview:_backView];

    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
    topView.backgroundColor = [UIColor orangeColor];
    //设置子类布局模式
    topView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [_backView addSubview:topView];
    [topView release];
    
    
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateFrame) userInfo:nil repeats:YES];
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

- (void) updateFrame
{
    _backView.frame = CGRectMake(_backView.frame.origin.x
                                 , _backView.frame.origin.y
                                 , _backView.frame.size.width + 5
                                 ,_backView.frame.size.height + 5);
}

view层操作

    // UIView 的层操作
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(10, 30, 300, 100)];
    topView.backgroundColor = [UIColor blackColor];
    [self.window addSubview:topView];
    [topView release];
    
    UIView *yellowView = [[UIView alloc]initWithFrame:CGRectMake(70, 90, 100, 100)];
    yellowView.backgroundColor = [UIColor yellowColor];
    [self.window addSubview:yellowView];
    [yellowView release];
    
    //先加入的view在下层,后加入的view在上层
    UIView *blueView = [[UIView alloc]initWithFrame:CGRectMake(10, 30, 100, 100)];
    blueView.backgroundColor = [UIColor blueColor];
    [self.window addSubview:blueView];
    [blueView release];
    
    //操作view的 由父类view来操作
    //将一个view放到下层
    [self.window sendSubviewToBack:blueView];
    //放到上层
    [self.window bringSubviewToFront:topView];
    
    //插入指定view到制定层
    UIView *greenView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 200)];
    greenView.backgroundColor = [UIColor greenColor];
    [self.window insertSubview:greenView aboveSubview:topView];
    [greenView release];
    
    //交换层
    [self.window exchangeSubviewAtIndex:2 withSubviewAtIndex:3];
    
    //subviews array 交换层,也会改变subview在数组中的位置
    // top--yellow -- blueview --
    //
    NSArray *subViews = self.window.subviews;
    UIView *view = [subViews objectAtIndex:2];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值