【发布时间】:2011-06-28 07:14:29
【问题描述】:
程序员朋友们,您好,
自从我尝试解决此问题以来已经有几个小时了。我计划创建某种与 Facebook 的 iOS 应用程序相同的菜单视图。问题是,我在UIScrollView 中最多只能添加两个UIButtons,第三个和其他连续按钮不会出现。我希望你能帮我解决这个问题。
这里是代码块:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = NO;
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
if(i == 0)
{
UIButton *button = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; //Creates a UIButton
button.frame = CGRectMake(25, 65, 70, 70); //sets the coordinates and dimensions of UIButton
button.backgroundColor = [UIColor clearColor]; //sets the background color
[button addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside]; //sets the target and action for the button
[button setBackgroundImage:[UIImage imageNamed:@"Message Board Icon-57.png"] forState:UIControlStateNormal]; //sets the background Image
[subview addSubview:button];
[button release];
UIButton *button2 = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; //Creates a UIButton
button2.frame = CGRectMake(125, 175, 70, 70); //sets the coordinates and dimensions of UIButton
button2.backgroundColor = [UIColor clearColor]; //sets the background color
[button2 addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside]; //sets the target and action for the button
[button2 setBackgroundImage:[UIImage imageNamed:@"Profile Page Icon-57.png"] forState:UIControlStateNormal]; //sets the background Image
[subview addSubview:button2];
[button2 release];
UIButton *button3 = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; //Creates a UIButton
button3.frame = CGRectMake(125, 65, 70, 70); //sets the coordinates and dimensions of UIButton
button3.backgroundColor = [UIColor clearColor]; //sets the background color
[button3 addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside]; //sets the target and action for the button
[button3 setBackgroundImage:[UIImage imageNamed:@"iCalendar Icon-57.png"] forState:UIControlStateNormal]; //sets the background Image
[subview addSubview:button3];
[button3 release];
}
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}
【问题讨论】:
-
将类型更改为圆角矩形时是否显示按钮?
-
您必须将 UIview *subview 添加到 scrollView 以在其中显示它以及按钮。就这样,[scrollview addsubview:subview];
-
我现在已经能够发现问题了。我的图像名称中有一些拼写错误,这导致它们被添加到 UIScrollView 中的不一致...感谢@dasdom 和@iphonePlayer 花时间阅读我的担忧。
-
将该答案设为正确,以便其他人可以从您的经验中受益。
-
@dasdom 当然可以。对于像我这样的低代表用户还有时间限制。我会在 6 小时后更新。
标签: iphone ios uiscrollview uibutton