0

私はこれに苦労しており、スクロールビューの仕組みを理解していない可能性があります。「self.description」の高さを自動的に計算しようとしていますが、何もうまくいかないようです。frame.size.height を使用して値を変更しようとしても、scrollviewframe から値を変更しました。ただし、高さはまだ計算されません。何か不足していますか?

- (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
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 Height よりも小さいときに scrollView のバウンスを有効にしたい場合は、これを使用します。scrollView.alwaysBounceVertical = YES;

于 2013-10-10T19:53:37.733 に答える