Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically creating Views in IOS (how does it work)?

A little background: I'm going through the CS193P iTune videos and I was stuck on the assignment 3 for the longest time. Basically, the assignment asks you to programmatically create a custom view to display a shape on the screen. I'm not using any view controllers by the way.

I could not get my view to display until I finally dragged a View object in Interface Builder and change the object name to my custom view class. So my question is when people say to programmatically create a view, are they just saying manually create the class but when you need to display it use IB? I can't help feeling like I misunderstood something?

edit: let me be more clear. My custom view has been initialized with a frame of 0, 0, 200, 150 and drawRect is overriden to draw a square in it. My view doesn't even show up if try adding it to the main window within my controller:

    UIWindow* window = [UIApplication sharedApplication].keyWindow;
[window addSubview:polygonView];

However, if use drag a view in IB and change the class to my view class, it shows up fine.

Edit: Added some code. This is my controller's awakeFromNib method where the view should be drawn.

    - (void)awakeFromNib {
    shape = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLable.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
    polygonView = [[PolygonView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    polygonView.backgroundColor = [UIColor blackColor];
    [window addSubview:polygonView];
    [self updateInterface];  
}

Part of my controller's updateInterface method:

- (void)updateInterface {
    [polygonView setPolygon:shape];
    [polygonView setNeedsDisplay];
...
}

PolygonView.h

#import <UIKit/UIKit.h>
#import "PolygonShape.h"

@interface PolygonView : UIView {
    IBOutlet PolygonShape *polygon; 
}

@property (readwrite, assign) PolygonShape *polygon;

- (void)drawRect:(CGRect)rect;
@end

PolygonView.m

#import "PolygonView.h"

@implementation PolygonView
@synthesize polygon;

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        nslog(@"initialized");
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
       CGRect bounds = [self bounds];

    [[UIColor grayColor] set];
    UIRectFill(bounds);

    CGRect square = CGRectMake(10, 10, 10, 100);
    [[UIColor blackColor] set];
    UIRectFill(square);

    [[UIColor redColor] set];
    UIRectFill(square);
    NSLog(@"drawRect called");
}
@end

The polygonView is being initialized but the drawRect isn't being called.

like image 926
subject_x Avatar asked Jan 27 '11 18:01

subject_x


People also ask

What is a view in iOS?

Views and controls are the visual building blocks of your app's user interface. Use them to draw and organize your app's content onscreen. Views can host other views.

What is UI view in iOS?

An object that manages the content for a rectangular area on the screen. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

How do you create a view in Objective C?

Create new “View” file from the User Interface menu. This will be your xib that represents a person in your app (let's say it's a dating app). Call it “PersonXIB” (or whatever you want). You're going to see what looks very much like a single view storyboard file with a blank view.

How do I present a UIView in Swift?

present the gray UIView like you would usually present a ViewController (appearing bottom up and user can slide down to dismiss). The bottomBar is a ContainerView and should not change by switching between the VC's, only the gray UIView which you can see in the 2nd picture.


1 Answers

To be even more specific to your question, the syntax would be

UIWindow* window = [UIApplication sharedApplication].keyWindow;

UIView *polygonView = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 200, 150)];
//add code to customize, e.g. polygonView.backgroundColor = [UIColor blackColor];

[window addSubview:polygonView];
[polygonView release];

This is a pattern you will use for not only this but subviews afterwards. Also, another note is with many of the templates, the viewController is already set up with it's own view. When you want to make a custom view, you create it like above but instead of the method above you set the viewControllers view to the newly created view like so:

viewController.view = polygonView;

Good luck!

like image 96
SushiGrass Jacob Avatar answered Oct 20 '22 11:10

SushiGrass Jacob