使用自动布局和CGContext绘图方法?

时间:2013-09-09 21:33:31

标签: ios6 autolayout xcode4.6 cg

我有一个现有的iPad应用程序(XCode 4.6,ARC,Storyboards和iOS 6.2)。该应用程序在纵向模式下完美运行,但在使用自动布局的横向模式下效果不佳(请参见下图)。

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

上图显示肖像模式,这是正确的。

enter image description here

这是横向模式......注意它缺少名称和背景颜色。

这是创建顶部网格的代码:

-(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];

}

我的问题是:使用CG绘图方法时,我是否需要做些不同的事情?左网格,顶部网格和中心网格都以相同的方式绘制。背景颜色位于图纸下方的UIView上。顶部网格的唯一约束是宽度和高度。

1 个答案:

答案 0 :(得分:1)

以下是我要找的答案:

  

核心图形绘图对视图系统一无所知(包括自动布局)。如果您正在绘制的内容没有出现,很可能是因为您的视图几何体导致传递的上下文不会产生您的绘图所期望的几何体(并且通常的解决方案是让您的绘图代码查看视图的基于那个的界限和位置内容。