0

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

这是显示纵向模式; 注意名称在黄色视图上方的位置

上图显示的是纵向模式,这是正确的。

在此处输入图像描述

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

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

-(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 上。顶部网格的唯一约束是宽度和高度。

4

1 回答 1

1

这是我一直在寻找的答案:

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

于 2013-09-12T03:18:11.553 回答