2013-08-28 92 views
1

您好我正在測試繪圖並創建一個簡單的滾動視圖,並嵌入子視圖,而不使用接口生成器。當我將所有東西都放在原點時,它會正確顯示,但如果我將所有東西都移動到y = 40以下,它就會消失。或者,滾動視圖和子視圖可以在我放置它們的任何位置獨立顯示,即。而不將視圖設置爲子視圖。任何人都可以解釋爲什麼會發生?UIScrollview不顯示

感謝

視圖要被嵌入:

@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

對不起,我白癡。嵌入式視圖幀正在被從渦旋幀的原點向下推

這些應該是

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