0

I am trying to make a clean implementation of programmatically created UI.

I start off with my AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];


    MainViewController *mainVC = [[MainViewController alloc] init];

    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:mainVC];

    self.window.rootViewController = navC;

    [self.window makeKeyAndVisible];
    return YES;

}

Then the MainViewController.m is a subclass of UIViewController implements the following:

- (void)loadView {

    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
    MenuView *contentView = [[MenuView alloc] initWithFrame:applicationFrame];
    self.view = contentView;

}

And the custom UIView in MenuView.m implements the following

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        NSLog(@"init got called");
        NSLog(@"frame size %f %f", self.frame.size.width, self.frame.size.height);
        self.backgroundColor = [UIColor greenColor];
    }
    return self;
}

 ...

- (void)loadView {

    NSLog(@"loadView got called");

    UIButton *newButton = [[UIButton alloc] init];
    newButton.titleLabel.text = @"New Button";
    newButton.backgroundColor = [UIColor blueColor];
    [self addSubview:newButton];

    NSDictionary *views = NSDictionaryOfVariableBindings(newButton);

    [newButton setTranslatesAutoresizingMaskIntoConstraints:NO];

    NSDictionary *metrics = @{@"buttonWidth": @(150), @"buttonHeight": @(150)};

    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(100)-[newButton(buttonWidth)]"
                                                                 options:0 metrics:metrics views:views]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[newButton(buttonHeight)]-(100)-|"
                                                                 options:0 metrics:metrics views:views]];

}

When I run this, the simulator shows me a green screen - but there is no button. The NSLogs in the init method fire and show a frame size of 320 x 548, but the loadView method does not get called. What am I doing wrong?

Thanks

Joseph
  • 9,171
  • 8
  • 41
  • 67
  • i think `loadView` called for `UIViewController` subclassed class not for custom `UIView` – Shankar BS Sep 03 '14 at 11:14
  • ok, where do I setup a view's subviews? – Joseph Sep 03 '14 at 11:15
  • 1
    try by giving a call to this button creation method during initialisation for example `- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { ....afert some code;[self createBUttonWithProperties]; }return self; }` and also change the method name `- (void)loadView` in custom `view` to some other name like `- (void)createBUttonWithProperties` – Shankar BS Sep 03 '14 at 11:17
  • 1
    yeah this works (I called it `-(void)setupSubviews`), but is it the "correct" way of doing it. I mean if there is a method like `loadView` that get's called anyway then I would prefer to use it. – Joseph Sep 03 '14 at 11:36

1 Answers1

0
- (void)loadView; 

is a method of UIViewController class, not UIView's.

So you need to setup its subviews inside init method where you already set background color.

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        // Initialization code
    }
    return self;
}
erkanyildiz
  • 13,044
  • 6
  • 50
  • 73