嗨,我正在测试绘图并创建了一个简单的滚动视图并嵌入了一个子视图,而不使用界面生成器。当我将所有内容定位在原点时,它会正确显示,但如果我将所有内容向下移动超过 y = 40,它就会消失。或者,滚动视图和子视图在我放置它们的任何位置独立显示良好,即。没有将视图设置为子视图。谁能解释为什么会发生这种情况?
谢谢
要嵌入的视图:
@implementation BLRView
- (id) initWithFrame:(CGRect) frame
{
self = [super initWithFrame:frame];
if(self)
{
[self setBackgroundColor:[UIColor blackColor]];
}
return self;
}
- (void) drawRect:(CGRect)dirtyRect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect bounds = [self bounds];
CGContextSetLineWidth(ctx, 30);
CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
float dashphase = 1.0;
float dashlengths[] = {1.0, 20.0};
CGContextSetLineDash(ctx, dashphase , dashlengths, 2);
CGContextMoveToPoint(ctx, bounds.origin.x, bounds.origin.y);
CGContextAddLineToPoint(ctx, bounds.size.width , bounds.origin.y);
CGContextStrokePath(ctx);
}
@end
并在 appdelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect screenRect = [[self window] bounds];
//this displays
CGRect viewFrame = CGRectMake(0, 0, screenRect.size.width * 3, 50);
CGRect scrollFrame = CGRectMake(0, 0, screenRect.size.width, 50);
//this is slightly cut off at the bottom
//CGRect viewFrame = CGRectMake(0, 30, screenRect.size.width * 3, 50);
//CGRect scrollFrame = CGRectMake(0, 30, screenRect.size.width, 50);
//this isnt shown
//CGRect viewFrame = CGRectMake(0, 100, screenRect.size.width * 3, 50);
//CGRect scrollFrame = CGRectMake(0, 100, screenRect.size.width, 50);
UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:scrollFrame];
BLRView *view = [[BLRView alloc]initWithFrame:viewFrame];
[[self window] addSubview:scrollview];
[scrollview addSubview:view];
[scrollview setContentSize:viewFrame.size];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}