简体   繁体   中英

UIScrollview isn't displayed

Hi I'm testing out drawing and created a simple scrollview and embedded a subview without using interface builder. It displays correctly when I position everything at the origin but if I move everything down past y = 40 it disappears. Alternatively, the scrollview and subview independently display fine at whatever position I place them ie. without setting the view as a subview. Can anyone explain why this would happen?

thanks

the view to be embedded:

@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

and in the 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;
}

Sorry, i'm an idiot. The embedded view frame is being pushed down from the origin of the scroll frame

these should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM