I’m diving into iOS development and I’m trying to figure out how to control the size and layout of multiple child views (in this case, two child views). I posted this question in a different context and my question wasn’t getting it answered, so I’m reposting it as a much simpler question. I’m attempting to add to children view and position them at (0, 0) and (100, 100) respectively, but the first view (master) I add ends up filling the whole screen. My code is very simple, what am I doing wrong that is preventing me from controlling the size and position of the two children views?
MySplitViewController.m
#import "MySplitViewController.h"
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MYSplitViewController (){}
@property (nonatomic, strong) MasterViewController *masterViewController;
@property (nonatomic, strong) DetailViewController *detailViewController;
@end
@implementation MySplitViewController
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
self.masterViewController = [[MasterViewController alloc] initWithNibName:nil bundle:nil];
self.detailViewController = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.frame = CGRectMake(0, 0, 768, 1004);
[self.view addSubview:self.masterViewController.view];
[self.masterViewController viewDidLoad];
[self.view addSubview:self.detailViewController.view];
[self.detailViewController viewDidLoad];
}
@end
MasterViewController.m
#import "MasterViewController.h"
@interface MasterViewController ()
@end
@implementation MasterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, 1004) style:UITableViewStylePlain];
table.dataSource = self;
table.delegate = self;
[self.view addSubview:table];
}
@end
DetailViewController.m
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
UIView *uiview = [[UIView alloc] initWithFrame:CGRectMake(100, 0, 668, 1004)];
[self.view addSubview:uiview];
}
@end
Thanks so much in advance for your wisdom!
Try setting the frames for
self.masterViewController.viewandself.detailViewController.viewbefore adding as subview toself.view.For eg:-
And you are not supposed to call
viewDidLoadmethod directly. It gets called automatically once the view is loaded on the screen.