我正在嘗試創建類似於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];
感謝Diederik的迴應。問題是按鈕的框架是相對於UIView的,因爲UIScrollView是視圖的子視圖。所以,按鈕的框架是相對於UIScrollView的超級視圖。也許我錯過了什麼?謝謝! – 2011-02-27 14:40:06