0

我将如何动态调整 a 的高度UIScrollView?基本上我UILabels创建了一堆(UILabels 的确切数量是随机的),并且UIScrollView是自动调整其高度以适应UILables. 这是我迄今为止在viewDidLoad.

- (void)viewDidLoad
{
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320
                                    , 1500)];
scroller.indicatorStyle = UIScrollViewIndicatorStyleWhite;
{ 

这是创建额外 UILavels 的操作

-(IBAction)scheduale{
int i;
for(i=0; i<[self retrieveTime] ; i++){
    //Add time label
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.frame = CGRectMake(10, (i+1) * 21, 31, 20);
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.backgroundColor = [UIColor colorWithRed:76.0/225.0 green:76.0/225.0 blue:76.0/225.0 alpha:1.0];
    NSString *labelString;
    labelString = [[NSNumber numberWithInt:i] stringValue];
    timeLabel.text = labelString;
    timeLabel.textAlignment = UITextAlignmentCenter;
    //theLabel.tag = (i+1) * 100;
    [scroller addSubview:timeLabel];
}
4

2 回答 2

2

您需要保留一个变量来跟踪您添加的标签数量及其高度,以便在您完成创建标签时设置内容大小。请参阅下面的调整代码:

-(IBAction)scheduale{

 int i; 
 int contentSize = 0;

 for(i=0; i<[self retrieveTime] ; i++){

     UILabel *timeLabel = [[UILabel alloc] init];
     timeLabel.frame = CGRectMake(10, (i+1) * 21, 31, 20);
     contentSize += 20;
     timeLabel.textColor = [UIColor whiteColor];
     timeLabel.backgroundColor = [UIColor colorWithRed:76.0/225.0 green:76.0/225.0 blue:76.0/225.0 alpha:1.0];
     NSString *labelString;
     labelString = [[NSNumber numberWithInt:i] stringValue];
     timeLabel.text = labelString;
     timeLabel.textAlignment = UITextAlignmentCenter;
     //theLabel.tag = (i+1) * 100;
     [scroller addSubview:timeLabel]; }

 [scroller setContentSize:CGSizeMake(320, contentSize)];
于 2012-04-05T02:26:04.057 回答
0

我同意@Kyle,但内容大小将随着标签的位置而增加,以最后一个位置+最后一个高度结束。所以他的想法需要稍微调整一下。

另外,只是为了确认:@world Peace - 你想改变滚动条的框架,还是只是它的内容大小?@Kyle 是正确的,您至少必须更改内容大小。

这是对该代码的一些清理:

#define kButtonWidth   31.0
#define kButtonHeight  20.0
#define kMargin         1.0

-(IBAction)scheduale {

    int i;
    CGFloat contentPositionY = 0.0;

    UIColor *labelColor = [UIColor colorWithRed:76.0/225.0 green:76.0/225.0 blue:76.0/225.0 alpha:1.0];

    for(i=0; i<[self retrieveTime] ; i++){
        //Add time label
        UILabel *timeLabel = [[UILabel alloc] init];

        timeLabel.frame = CGRectMake(0, contentPositionY, kButtonWidth, kButtonHeight);
        contentPositionY += kButtonHeight + kMargin;

        timeLabel.textColor = [UIColor whiteColor];
        timeLabel.backgroundColor = labelColor;

        timeLabel.text = [NSString stringWithFormat:@"@d", i];

        timeLabel.textAlignment = UITextAlignmentCenter;
        //theLabel.tag = (i+1) * 100;
        [scroller addSubview:timeLabel];
    }

    // now size the content
    scroller.contentSize = CGSizeMake(kButtonWidth, contentPositionY + kButtonHeight);

    // and to get the margins you built into the loop calculation
    scroller.contentInset = UIEdgeInsetsMake(21, 10, 0, 0);   // your code implied these values
}
于 2012-04-05T03:02:03.447 回答