2011-02-27 89 views
1

我正在嘗試創建類似於this video中的水平滾動菜單。UIScrollView中的UIView不出現

由於某種原因,UIView在向其中添加一堆UIButtons並將其添加到UIScrollView後未出現。這裏是我的代碼(這就是所謂的在UIViewController子類的-viewDidLoad):

//set up scrollview 
UIScrollView *designPicker = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)]; 

//set up a view to drop into the scroll view 
UIView * buttonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 431, 640, 49)]; 

//add buttons to scrollview 
// load all the images from our bundle and add them to the scroll view 
NSUInteger i; 
float runningX = designPicker.frame.origin.x; 
for (i = 1; i <= 10; i++) 
{ 
    UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [tempBtn setBackgroundImage:[UIImage imageNamed:@"button.png"] forState:UIControlStateNormal]; 
    CGRect rect = CGRectMake(runningX, designPicker.frame.origin.y, 30.0, 30.0); 
    tempBtn.frame = rect; 
    [buttonsView addSubview:tempBtn]; 
    runningX = runningX + 35; 
    [tempBtn release]; 
} 

[designPicker setContentSize:buttonsView.frame.size]; 
[designPicker addSubview:buttonsView]; 
[self.view addSubview:designPicker]; 

回答

1

不要添加使用UIScrollView的幀的按鈕。框架的原點位於超視圖的(UIScrollView的超視圖)座標中。你應該使按鈕的框架相對於UIView。所以如果你想讓按鈕出現在視圖的頂部,你應該從(0,0)開始。

+0

感謝Diederik的迴應。問題是按鈕的框架是相對於UIView的,因爲UIScrollView是視圖的子視圖。所以,按鈕的框架是相對於UIScrollView的超級視圖。也許我錯過了什麼?謝謝! – 2011-02-27 14:40:06

0

不是將scrollview作爲子視圖添加到視圖中,而是將視圖添加爲scrollview的子視圖。 As-

[self.scrollView addSubview:self.view]; 
// release scrollView as self.view retains it 
self.view=self.scrollView; 
[self.scrollView release]; 

並確保您的視圖應該有比您scrollview的內容大小大的內容大小。它爲我工作。