chipbk10
chipbk10

Reputation: 5955

UIView missing background

My application's structure looks like:

UIViewController1 contains UIViewController2, and I add some UIView, loading from Xib, to the UIViewController2 with the following code:

SessionView* b = [[[NSBundle mainBundle] loadNibNamed:@"SessionView" owner:self options:nil] lastObject];
[self.view addSubview:b];

//- set Frame before or after addSubView, there is no difference
CGRect frame = b.frame;
frame.origin.x = 0;
frame.origin.y = 0;
b.frame = frame;

The original UIView looks like this:

Original UIView

After adding to the UIViewController, it looks like this:

After Adding

With the following code, when I assign the width and height of the UIView equal to a part of the width and height of the main view, it looks good


SessionView* b = [[[NSBundle mainBundle] loadNibNamed:@"SessionView" owner:self options:nil] lastObject];

    [self.view addSubview:b];
    CGRect frame = b.frame;
    frame.origin.x = 0;
    frame.origin.y = 0;

    //------- Assign the Width and Height Value
    //-------------------------------------------
    frame.size.width = CGRectGetWidth(self.view.bounds) / 3;
    frame.size.height = CGRectGetHeight(self.view.bounds) / 3;

    b.frame = frame;

acceptable

Upvotes: 0

Views: 203

Answers (1)

Dipen Panchasara
Dipen Panchasara

Reputation: 13600

It seems you are adding viewController directly as subview, instead you should be adding view of viewController as subview.

SessionView* b = [[[NSBundle mainBundle] loadNibNamed:@"SessionView" owner:self options:nil] lastObject];
[self.view addSubview:b.view];

Upvotes: 0

Related Questions