子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];