0

我一直在为此苦苦挣扎,也许我不了解滚动视图的工作原理。我正在尝试自动计算“self.description”的高度,但我所做的一切似乎都不起作用。我已经更改了滚动视图框架的值,甚至尝试使用 frame.size.height 更改值。但是它仍然不计算高度。我错过了什么吗?

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

    if (self) {

        CGRect scrollViewFrame = CGRectMake(0, 0, 460, 590);
        scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];

        [self.contentView addSubview:self.scrollView];


        [scrollView setCanCancelContentTouches:NO];

        scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
        scrollView.clipsToBounds = YES;
        scrollView.scrollEnabled = YES;
        scrollView.pagingEnabled = NO;

        self.label = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 67.0, 290, 50)];
        self.label.font= [UIFont fontWithName:@"Helvetica-Bold" size:20];
        self.label.textColor = [UIColor colorWithRed:33.0f/255.0f green:74.0f/255.0f blue:146.0f/255.0f alpha:1.0f];
        self.label.numberOfLines = 0;
        self.label.lineBreakMode = NSLineBreakByWordWrapping;
        self.label.backgroundColor = [UIColor whiteColor];
        [scrollView addSubview:self.label];

        //[self.contentView addSubview:self.label];

        self.description = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 380.0, 300, 9999)];
        self.description.font= [UIFont fontWithName:@"Helvetica-Light" size:15];
        self.description.textColor = [UIColor blackColor];
        self.description.textAlignment = NSTextAlignmentJustified;
        self.description.numberOfLines = 0;
        self.description.lineBreakMode = NSLineBreakByWordWrapping;
        self.description.backgroundColor = [UIColor whiteColor];
        [scrollView addSubview:self.description];
        //[self.contentView addSubview:self.description];


        self.image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 130, 280, 228)];
        self.image.clipsToBounds = YES;
        [scrollView addSubview:self.image];
        //[self.contentView addSubview:self.image];

        float hgt=0; for (UIView *view in scrollView.subviews) hgt+=view.frame.size.height;
        [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width,hgt)];


        self.backgroundColor = [UIColor whiteColor];

    }
}
4

2 回答 2

0

您是否要调整滚动视图的大小或滚动的内容大小?

如果您想调整滚动视图的大小,则不会调整 self.contentView 的大小,如果它不大于或等于滚动视图,则可能是您的问题。

于 2013-10-10T19:19:32.520 回答
0
float contentHeight = 0;

contentHeight = yourLastSubview.frame.origin.x + yourLastSubview.frame.size.height;

[scrollView setContentSize:CGSizeMake(scrollView.frame.size.width,contentHeight)];

//yourLastSubview is for example your self.description, because self.description' origin.y is the largest (380)

如果您scrollView.frame.size.height的大于contentHeight,您scrollView将不会滚动。例如,如果您的scrollView.frame = CGRectMake(0,0,320,480);和您的contentHeight为 400,则 scrollView 不会滚动,但会滚动contentHeight = 500;

如果您想在 contentHeight 小于您的 scrollView 高度时启用弹跳滚动视图,请使用以下命令:scrollView.alwaysBounceVertical = YES;

于 2013-10-10T19:53:37.733 回答