UIView 使用细节

引言:


# 一. 获取当前 view 所在的 ViewController

# 1. 获取:


- (UIViewController *)getCurrentViewController{
	for (UIView *next = [self superview]; next; next = next.superview) {
		UIResponder *nextResponder = [next nextResponder];
		if ([nextResponder isKindOfClass:[UIViewController class]]) {
			return (UIViewController*)nextResponder;
		}
	}
	return nil;
}

# 2. 使用:

UIViewController *vc = [self getCurrentViewController];

# 二. view 视图的层级关系

# 1. 添加到当前视图的前面:


// 将一个UIView显示在最前面只需要调用其父视图的 bringSubviewToFront() 方法。
[self.view bringSubviewToFront:view];

# 2. 添加到当前视图的最后面:


// 将一个UIView层推送到背后只需要调用其父视图的 sendSubviewToBack() 方法。
[self.view sendSubviewToBack:view];

# 三. 为 view 设置背景图片


/// 设置背景图片,先添加,然后设置为最后
UIImageView *customBackgournd = [UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.jpg"]];
[self.view addSubview:customBackgournd];
[self.view sendSubViewToBack:customBackgournd];

// 如果是用ib,就在xib文件中添加UIImageView,放在最下面。