我正在尝试创建一个类似于this video中的水平滚动菜单。
由于某些原因,在向UIView
添加一堆UIButtons
并将其添加到UIScrollView
后,-viewDidLoad
不会显示。这是我的代码(它在UIViewController
的子类的//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}}
答案 0 :(得分:1)
您不应使用UIScrollView
的框架添加按钮。框架的原点位于superview的(UIScrollView
)坐标的超视图中。您应该使按钮的框架相对于UIView
。因此,如果您希望按钮显示在视图的顶部,您应该从(0,0)开始。
答案 1 :(得分:0)
不是将scrollview作为子视图添加到视图中,而是将视图添加为scrollview的子视图。正如 -
[self.scrollView addSubview:self.view];
// release scrollView as self.view retains it
self.view=self.scrollView;
[self.scrollView release];
并确保您的视图的内容大小应该大于滚动视图的内容大小。它对我有用。