Question

I am attempting to subclass UIView to allow me to draw six small boxes on the single view controller for my app - the idea being I will display text using labels on top of these 6 boxes.

I have created a new objective-c class subclassing UIView, entered the code to set the size/colour/position of the boxes & finally applied the custom class to the 'View' in the my storyboard by replacing UIView with the name of my class in the Custom Class section of the identity inspector (this is a single view app btw) but when I run the app nothing appears (besides what was already in the storyboard!) - if I use the same code in a completely blank app the 6 boxes appear! So I guess I am missing something... here is part of my code for the boxes:

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(context, 0.611, 0.505, 0.321, 0.8);
    CGContextFillRect(context, CGRectMake(53, 271, 37, 52));
    CGContextFillRect(context, CGRectMake(141, 271, 37, 52));
    CGContextFillRect(context, CGRectMake(229, 271, 37, 52));
    CGContextFillRect(context, CGRectMake(52, 399, 37, 52));
    CGContextFillRect(context, CGRectMake(140, 399, 37, 52));
    CGContextFillRect(context, CGRectMake(229, 399, 37, 52));
}
Was it helpful?

Solution

From the description of what you want (not your problem), don't bother. Instead, add the labels (or text fields) as subviews and use their background colour and their layer border properties to create the UI look you want.

In your current code, drawRect: should be calling super.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top