将UIButton添加到水平滚动视图

时间:2011-11-10 22:50:42

标签: iphone objective-c ipad

我有以下代码:

 self.horizontalScrollView.delegate = self;
    self.horizontalScrollView.scrollEnabled = YES;
    self.horizontalScrollView.autoresizingMask = YES;

 int xOffset = 0;

    for(int index=0; index < 5; index++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTag:index];
        [button addTarget:self action:@selector(categoryChanged:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:@"TEST" forState:UIControlStateNormal];

        //CGSize expectedLabelSize = [[self.category objectAtIndex:index] sizeWithFont:[UIFont fontWithName:@"bebas" size:13.0]                     
        //                                                           constrainedToSize:CGSizeMake(300,9999)
        //                                                               lineBreakMode:UILineBreakModeWordWrap];
        //[button setFrame: CGRectMake(xOffset, 0, expectedLabelSize.width + 25, 31)];

        /*
        CGSize expectedLabelSize = [@"TEST" sizeWithFont:[UIFont fontWithName:@"ArialMT" size:13.0]                     
                                                                   constrainedToSize:CGSizeMake(300,9999)
                                                                       lineBreakMode:UILineBreakModeWordWrap];
        */
        int expectedLabelSize = 25;
        [button setFrame: CGRectMake(xOffset, 0, expectedLabelSize + 25, 31)];

        [self.horizontalScrollView addSubview:button];
         xOffset += expectedLabelSize + 25;
    }

不确定为什么我不能左右滚动?

1 个答案:

答案 0 :(得分:0)

您需要将大于其帧大小的CGSize传递到:

[horizontalScrollView setContentSize:]

然后您的滚动视图将滚动。