Question

I have an existing iPad app (XCode 4.6, ARC, Storyboards and iOS 6.2). The app works perfectly in Portrait mode, but not so well in Landscape mode using Auto Layout (see images below).

This is showing portrait mode; notice where the names are above the yellow view

The image above shows portrait mode, which is correct.

enter image description here

This is landscape mode... notice it's missing the names and background color.

This is the code that creates the top grid:

-(void) drawTopGrid  {

//  set up notification for redrawing topGrid
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(notificationToRedrawTopGrid:)
                                             name:@"redrawTopGrid" object:nil ];

//  setup for drawing grid
CGContextRef context = UIGraphicsGetCurrentContext();

#define START_VERTICAL_LINE  1  //  was 110
#define VERTICAL_LINE_INCREMENT 110  //  was 110
#define VERTICAL_LINE_LENGTH 52  // (compute based on number of hours)  

//  draw first vertical line
CGContextMoveToPoint(context, START_VERTICAL_LINE, 0); //start
CGContextAddLineToPoint(context, START_VERTICAL_LINE, VERTICAL_LINE_LENGTH); //draw to this point
CGContextStrokePath(context);  // draw it...

//  draw remainder of vertical lines (based on count of staff positions defined)  <--------------- TODO
for(int i = 2; i < 24; i += 2)  {  // will hold 6 staff positiions
    CGContextMoveToPoint(context, (i * VERTICAL_LINE_INCREMENT) - i, 0); //start
    CGContextAddLineToPoint(context, (i * VERTICAL_LINE_INCREMENT- i), VERTICAL_LINE_LENGTH); //draw to this point
    CGContextStrokePath(context);  // draw it...
}

//  get the staff names
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pathForStaffNames = [documentsDirectory stringByAppendingPathComponent:@"staffNames.plist"];
NSDictionary *staffDict = [NSDictionary dictionaryWithContentsOfFile:pathForStaffNames];
staffPos1 = [staffDict objectForKey:Slot1];
staffPos2 = [staffDict objectForKey:Slot2];
staffPos3 = [staffDict objectForKey:Slot3];
staffPos4 = [staffDict objectForKey:Slot4];
staffPos5 = [staffDict objectForKey:Slot5];
staffPos6 = [staffDict objectForKey:Slot6];

const float hourFontSize = 18;
UIFont *hourfont=[UIFont systemFontOfSize:hourFontSize];

//  draw staff names
[[UIColor redColor] set];
[staffPos1 drawAtPoint:CGPointMake(85, 12) withFont:hourfont];  //  increment is 220

[[UIColor blueColor] set];
[staffPos2 drawAtPoint:CGPointMake(303, 12) withFont:hourfont];

[[UIColor magentaColor] set];
[staffPos3 drawAtPoint:CGPointMake(523, 12) withFont:hourfont];

[[UIColor redColor] set];
[staffPos4 drawAtPoint:CGPointMake(743, 12) withFont:hourfont];

[[UIColor blueColor] set];
[staffPos5 drawAtPoint:CGPointMake(963, 12) withFont:hourfont];

[[UIColor magentaColor] set];
[staffPos6 drawAtPoint:CGPointMake(1183, 12) withFont:hourfont];

}

My question is: do I have to do something different when using CG drawing methods? The left grid, top grid and center grid are all drawn the same way. The background color is on the UIView below the drawing. The only constraints for the top grid are width and height.

Was it helpful?

Solution

Here's the answer I was looking for:

Core Graphics drawing knows nothing of the view system (including auto layout). If content you are drawing is not appearing, it is likely because your view geometry is causing a context to be passed in that is not producing the geometry that your drawing expects (and the usual solution there is for your drawing code to look at the view's bounds and position content based on that).

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