2013-10-10 17 views
0

我一直在努力,也許我不理解如何scrollview工作。我正在嘗試自動計算'self.description'的高度,但是我所做的沒有任何工作似乎。即使嘗試使用frame.size.height更改值,我也更改了scrollviewframe的值。但它仍然不計算高度。我錯過了什麼嗎?ScrollViews將不會調整

- (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]; 

    } 
} 

回答

0

您是否嘗試調整scrollview或滾動的內容大小?

如果您想調整scrollview的大小,self.contentView不會調整大小,如果它不大於或等於scrollview,可能是您的問題。

+0

我試圖調整self.description子視圖。內容大小 –

+0

而您想要根據文本長度調整大小?然後,調整滾動視圖,對不對? –

+0

正是我想要做的。我嘗試了很多不同的東西都無濟於事。 –

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.heightcontentHeight大,你scrollView不會滾動。例如,如果您的scrollView.frame = CGRectMake(0,0,320,480);和您的contentHeight是400 scrollView不會滾動,但如果contentHeight = 500;它會。

如果你想使你的彈跳滾動視圖時,則contentHeight比你的身高滾動視圖使用這種更小:scrollView.alwaysBounceVertical = YES;

+0

沒有運氣,滾動不是問題,它的self.description調整大小沒有完成。 –