我正在尝试创建一个类似于 this video 中的水平滚动菜单.
由于某些原因,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];
最佳答案
您不应使用 UIScrollView
的框架添加按钮。框架的原点位于父 View (UIScrollView
的父 View )坐标中。您应该使按钮的框架相对于 UIView
。因此,如果您希望按钮出现在 View 的顶部,您应该从 (0,0) 开始。
关于ios - UIScrollView 中的 UIView 没有出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5131910/