UIScrollview不会显示

时间:2013-08-28 09:26:32

标签: iphone ios objective-c uiscrollview

您好我正在测试绘图并创建了一个简单的scrollview并嵌入了一个子视图而不使用界面构建器。当我将所有东西放在原点时它会正确显示但是如果我将所有东西都移动到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;
}

1 个答案:

答案 0 :(得分:1)

抱歉,我是个白痴。嵌入的视图框架正从滚动框架的原点向下推动

这些应该是

CGRect viewFrame = CGRectMake(0, 0, screenRect.size.width * 3, 50);
CGRect scrollFrame = CGRectMake(0, 30, screenRect.size.width, 50);