第三个和其他连续的UIButton不会出现在UIScrollView中

时间:2011-06-28 07:14:29

标签: iphone ios uiscrollview uibutton

美好的一天程序员,

自从我尝试解决此问题以来,已经过了几个小时。我打算创建一些与Facebook的iOS应用程序相同的菜单视图。问题是,我只能在UIButtons中添加两个UIScrollView,第三个和其他连续按钮不会显示。我希望你能帮助我。

以下是代码块:

- (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);

}

1 个答案:

答案 0 :(得分:0)

您必须将UIView *subview添加到scrollView以在其中显示该按钮。只是做,[scrollView addSubview:subView];