iOS – Subclassing UIView

Moving on to Chapter 6 in Joe Conway’s book, this chapter takes us further from previously using UIButton and UILabel to UIView. This is illustrated with the Hypnosister App, which draws concentric circles on the screen programmatically. I have a bittersweet taste towards creating artwork programatically, only because as a Photoshop wonk, I like having an excuse to use it. However, I get that it reduces file sizes and the like. It just seems like a lot of writing just to draw some circles:

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions


CGRect wholeWindow = [window bounds];

UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:wholeWindow];

[window addSubview:scrollView];

// Make your view twice as large as the window
CGRect reallyBigRect;

reallyBigRect.origin = CGPointZero;

reallyBigRect.size.width = wholeWindow.size.width * 2;

reallyBigRect.size.height = wholeWindow.size.height * 2;

[scrollView setContentSize:reallyBigRect.size];

// Center it in the scroll view
CGPoint offset;

offset.x = wholeWindow.size.width * 0.5;

offset.y = wholeWindow.size.height * 0.5;

[scrollView setContentOffset:offset];

// Enable zooming
[scrollView setMinimumZoomScale:0.5];

[scrollView setMaximumZoomScale:5];

[scrollView setDelegate:self];

// Create the view
view = [[HypnosisView alloc] initWithFrame:reallyBigRect];

[view setBackgroundColor:[UIColor clearColor]];

[scrollView addSubview:view];

[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];

[window makeKeyAndVisible];

return YES;


Again, I’m struggling to fully comprehend the idea behind classes, sub-classes and the act of “subclassing”. I keep trying to relate them back to CSS classes, and I’m not sure that’s an appropriate metaphor, which may be causing more confusion than it should.

Want to know when stuff like this is published?
Sign up for my email list.

Photo of Justin Harter


Justin has been around the Internet long enough to remember when people started saying “content is king”.

He has worked for some of Indiana’s largest companies, state government, taught college-level courses, and about 1.1M people see his work every year.

You’ll probably see him around Indianapolis on a bicycle.

Leave a Comment