ScrollViews不会调整大小

时间:2013-10-10 19:11:04

标签: ios uiscrollview

我一直在努力解决这个问题,也许我不了解scrollviews的工作原理。我试图自动计算'self.description'的高度,但我做的事似乎没有用。我已经尝试从scrollviewframe更改值,甚至尝试使用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];

    }
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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,请使用:scrollView.alwaysBounceVertical = YES;