The situation
I add a view (loaded by a view controller) to another view. After having added it, the view has a size
equal to (0,0)
. I don't understand why and I would like to know.
I know how to solve this problem (with constraints, either created automatically with view.translatesAutoresizingMaskIntoConstraints = YES
either created "by hand").
What I really want is understanding why, in this case, it does not work.
Code
Code of my main View Controller
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *mainView;
@property (nonatomic, strong, readwrite) ColorViewController * colorVC ;
@end
@implementation ViewController
+ (void)fillView:(UIView *)bigView
withView:(UIView *)view
{
view.translatesAutoresizingMaskIntoConstraints = NO ;
// view.translatesAutoresizingMaskIntoConstraints = YES ;
view.frame = bigView.bounds ;
[bigView addSubview:view] ;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.colorVC = [ColorViewController new] ;
[ViewController fillView:self.mainView
withView:self.colorVC.view] ;
}
@end
The associated storyboard
Capture d’écran 2015-04-30 à 17.25.32.png
Code of ColorViewController
@implementation ColorViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
self.view.backgroundColor = color ;
}
@end
Its XIB
Log
If I log the view (of ColorViewController
), I get:
2015-04-30 17:33:58.329 TEST_SUBVIEWS[51745:602214] <UIView: 0x7b4191e0; frame = (-179 -236; 0 0); autoresize = RM+BM; layer = <CALayer: 0x7b418ce0>>
The strangest things
What is really strange in this is that if I remove the only object in the above XIB (the
UILabel
), then, everything works well.Also, if I create the view with
[UIView new]
and then use the same method to add it to the "main view", then everything works well.
Strange?