在一個uiViewController中加載一個通過xib創建的uiView,結果界面不正確

背景:我有一個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都不對呢?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章