背景:我有一个uiViewController,是创建的时候带有xib的,需要在这个VC的view上再加一个uiView,而这个uiView是也是通过xib创建的,在xib中画了几个控件,之间有约束,然后我在VC中是这样写的
uiViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:WhiteColor];
[self initBottomView];
[self initImageShowView];
[self initButton];
brightView = [[[NSBundle mainBundle]loadNibNamed:@"ImageBrightView" owner:self options:nil] lastObject];
brightView.imageView = _imageShowView;
[self.view addSubview:brightView];
}
uiView的代码如下:
- (void)awakeFromNib
{
self.backgroundColor = RedColor;
// view的位置,人工计算
iHeight = ScreenHeight - ScreenWidth;
self.frame = CGRectMake(0, ScreenWidth, ScreenWidth, iHeight);
_topViewHeight.constant = iHeight - BottomViewHeight - LineViewHeight - MiddleViewHeight - LineViewHeight;
[self initSlide];
//滤镜相关
_context=[CIContext contextWithOptions:nil];
_colorControlsFilter=[CIFilter filterWithName:@"CIColorControls"];
_entity = [MEPAManager sharedPAManager].pictureEntityArray[0];
_image = [CIImage imageWithCGImage:_entity.fullOriginImg.CGImage];
[_colorControlsFilter setValue:_image forKey:kCIInputImageKey];
}
生成的界面完全是错的,如下:
调试了很久,不管是在uiView中还是在uiViewController中,去设置子uiView的frame,界面显示出来的子View的位置和大小都是错的。
但是,通过另一种方法做就是对的,VC上增加一个按钮,当VC显示完成后,我点击按钮,把这个uiView再添加上去,代码如下:
-(void)ClickButton
{
ImageBrightView * brightView = [[[NSBundle mainBundle]loadNibNamed:@"ImageBrightView" owner:self options:nil] lastObject];
// CGFloat width = ScreenWidth;//self.view.frame.size.width;
// brightView.frame = CGRectMake(0, width, width, ScreenHeight - width);
brightView.backgroundColor = RedColor;
NSLog(@"width:%f, height:%f", brightView.frame.size.width, brightView.frame.size.height);
brightView.imageView = _imageShowView;
[self.view addSubview:brightView];
}
这样出来的效果就是对的:
疑问:这是为什么呢?当VC渲染好了之后我再去加uiView,这种正常的我可以理解,但为什么第一种方法死活添加的子view都不对呢?