2011-09-10 6 views
0

3つのUIScrollViewControllerを3つの水平ストライプと同じ画面に表示しようとしています。これを達成するためのコードは以下のとおりですが、何らかの理由で上のストライプのみを表示するように動作しません。複数のUIScrollViewControllerを同じウィンドウに表示します。一部のビューでは表示しません。

私はウインドウに置かれた追加の白い背景の助けによって、画面の他の2つの領域もスクロール可能ですが、何らかの理由でそれらの背景色が表示されません...私は '理由を理解できない。ここで

はコードです:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Creating the window programmatically 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

    CGRect bounds = [self window].bounds; 

    float x = bounds.origin.x; 
    float y = bounds.origin.y; 
    float w = bounds.size.width; 
    float h = bounds.size.height; 

    CGRect upperFrame = CGRectMake(x,y,w,h/3); 
    CGRect middleFrame = CGRectMake(x,y + h/3,w,h/3); 
    CGRect lowerFrame = CGRectMake(x, y + 2 * h/3, w, h/3); 

    // Creating the scroll view which will contain the 3 views 
    UIScrollView *upperScrollView = [[UIScrollView alloc] initWithFrame:upperFrame]; 
    UIScrollView *middleScrollView = [[UIScrollView alloc] initWithFrame:middleFrame]; 
    UIScrollView *lowerScrollView = [[UIScrollView alloc] initWithFrame:lowerFrame]; 

    // ContentSize should be wide enough for 3 pages 
    [upperScrollView setContentSize:CGSizeMake(3 * w, h/3)]; 
    [middleScrollView setContentSize:CGSizeMake(3 * w, h/3)]; 
    [lowerScrollView setContentSize:CGSizeMake(3 * w, h/3)]; 

    // Enforce the display of only one page at a time 
    [upperScrollView setPagingEnabled:YES]; 
    [middleScrollView setPagingEnabled:YES]; 
    [lowerScrollView setPagingEnabled:YES]; 

    // UPPER SCROLLVIEW 

    // This will be the first view (red) 
    UIView *redViewU = [[UIView alloc] initWithFrame:upperFrame]; 
    [redViewU setBackgroundColor:[UIColor redColor]]; 

    // The second view will start horizontally when the first view ends 
    upperFrame.origin.x += w; 

    // This will be the second view (green) 
    UIView *greenViewU = [[UIView alloc] initWithFrame:upperFrame]; 
    [greenViewU setBackgroundColor:[UIColor greenColor]]; 

    // The third view will start horizontally when the second view ends 
    upperFrame.origin.x += w; 

    // This will be the third view (blue) 
    UIView *blueViewU = [[UIView alloc] initWithFrame:upperFrame]; 
    [blueViewU setBackgroundColor:[UIColor blueColor]]; 

    // Adding the 3 views to the scroll view 
    [upperScrollView addSubview:redViewU]; 
    [upperScrollView addSubview:greenViewU]; 
    [upperScrollView addSubview:blueViewU]; 

    // Now creating the view controller, father of the scrollview 
    UIViewController *upperViewController = [[UIViewController alloc] init]; 
    [upperViewController setView:upperScrollView]; 

    // MIDDLE SCROLLVIEW 

    // This will be the first view (red) 
    UIView *redViewM = [[UIView alloc] initWithFrame:middleFrame]; 
    [redViewM setBackgroundColor:[UIColor redColor]]; 

    // The second view will start horizontally when the first view ends 
    middleFrame.origin.x += w; 

    // This will be the second view (green) 
    UIView *greenViewM = [[UIView alloc] initWithFrame:middleFrame]; 
    [greenViewM setBackgroundColor:[UIColor greenColor]]; 

    // The third view will start horizontally when the second view ends 
    middleFrame.origin.x += w; 

    // This will be the third view (blue) 
    UIView *blueViewM = [[UIView alloc] initWithFrame:middleFrame]; 
    [blueViewM setBackgroundColor:[UIColor blueColor]]; 

    // Adding the 3 views to the scroll view 
    [middleScrollView addSubview:redViewM]; 
    [middleScrollView addSubview:greenViewM]; 
    [middleScrollView addSubview:blueViewM]; 

    // Now creating the view controller, father of the scrollview 
    UIViewController *middleViewController = [[UIViewController alloc] init]; 
    [middleViewController setView:middleScrollView]; 

    // LOWER SCROLLVIEW 

    // This will be the first view (red) 
    UIView *redViewL = [[UIView alloc] initWithFrame:lowerFrame]; 
    [redViewL setBackgroundColor:[UIColor redColor]]; 

    // The second view will start horizontally when the first view ends 
    lowerFrame.origin.x += w; 

    // This will be the second view (green) 
    UIView *greenViewL = [[UIView alloc] initWithFrame:lowerFrame]; 
    [greenViewL setBackgroundColor:[UIColor greenColor]]; 

    // The third view will start horizontally when the second view ends 
    lowerFrame.origin.x += w; 

    // This will be the third view (blue) 
    UIView *blueViewL = [[UIView alloc] initWithFrame:lowerFrame]; 
    [blueViewL setBackgroundColor:[UIColor blueColor]]; 

    // Adding the 3 views to the scroll view 
    [lowerScrollView addSubview:redViewL]; 
    [lowerScrollView addSubview:greenViewL]; 
    [lowerScrollView addSubview:blueViewL]; 

    // Now creating the view controller, father of the scrollview 
    UIViewController *lowerViewController = [[UIViewController alloc] init]; 
    [lowerViewController setView:lowerScrollView]; 

    // A white background view to see at least the scroll indicators 
    UIView *whiteView = [[UIView alloc] initWithFrame:bounds]; 
    [whiteView setBackgroundColor:[UIColor whiteColor]]; 

    // Finally the window will hold the view controllers' views 
    [[self window] addSubview:whiteView]; 
    [[self window] addSubview:upperViewController.view]; 
    [[self window] addSubview:middleViewController.view]; 
    [[self window] addSubview:lowerViewController.view]; 

    // Displaying the window 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

答えて

0

解決:私はいくつかの理由でUIScrollViewの内部ビューは、相対的なxのを望んでいることを実現IBと同じことをやって成功した後、yはそのフレーム、絶対ではないの座標。 (すべてのUIScrollViewのために同じ)以下で上記のコード内のビューのフレームを変更する

CGRect redViewFrame = CGRectMake(x,y,w,h/3); 
CGRect greenViewFrame = CGRectMake(w,y,w,h/3); 
CGRect blueViewFrame = CGRectMake(2 * w,y,w,h/3); 

は、問題を解決し、そして今、私は3スクロールの背景を見ることができます。 それは誰かを助けてくれることを願っています:)私は今は終わっています。

関連する問題