Guilherme Campos Hazan
Guilherme Campos Hazan

Reputation: 1031

Implementing programmatically UIView over a UIViewController

I'm trying to understand which methods of UIView I must override to be able to display something.

I have a very simple application that:

  1. Has a single UIViewController

  2. Has a UIView

  3. The UIView creates an offscreen image with the size of the View and the image is drawn in drawRect

Questions:

  1. How should I create the view? Calling initWithRect? Or just init? I want to do it programatically, no storyboard nor nibs.

  2. What method of UIView is called when the UIViewController sets the view bounds? And how can I get these bounds (to be able to create the offscreen image).

  3. Does someone know where can I find the source for UIImageView?

Note: I've been trying to do this for 4 days, without success.

Upvotes: 0

Views: 1651

Answers (3)

Phillip Knezevich
Phillip Knezevich

Reputation: 1

You need to override -(void)loadView in your custom ViewController class:

UIView *baseView = [[UIView alloc] init];
self.view = baseView;
[super loadView];

From there you can create a custom UIView that adds the view property of that ViewController.

Upvotes: 0

user1452248
user1452248

Reputation: 767

That's how i m using UIView in UIViewController and added UIImageView and UITextView as subview of UIView in UIViewController didn't showed code for UITextview because you are not asking about it in your question.

- (void)viewDidLoad
{
    UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:baseView];
    [baseView release];

    // Displays UIImageView
    UIImageView* ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, 300, 235)];
    self.view.backgroundColor = [UIColor brownColor];

    // load all the frames of our animation
    ImageView.animationImages = [NSArray arrayWithObjects:    
                                    [UIImage imageNamed:@"1a.png"],
                                    [UIImage imageNamed:@"1b.png"],
                                    nil];

    // all frames will execute in 25 seconds
    ImageView.animationDuration = 25; 

    // start animating
    [ImageView startAnimating];
    ImageView.layer.borderWidth = 2;
    ImageView.layer.borderColor = [[UIColor whiteColor]CGColor];
    [ImageView.layer setMasksToBounds:YES];
    [ImageView.layer setCornerRadius:15.0f];

    [baseView addSubview:ImageView]; 
}

Hope it helps you.

Upvotes: 0

skram
skram

Reputation: 5314

1.) initWithFrame:

1.) By default, there is no method that's automatically called when bounds are set. If you subclass the UIView you can override the setBounds method if you wish.

2.) Sources for UIImageView?..The header class is available in the CoreFoundation.framework.

Upvotes: 3

Related Questions