调整视图大小。不按我计划的方式工作

时间:2012-01-21 16:47:06

标签: iphone objective-c ios uiview

我正在根据UITextViews移动的像素数量调整UIView和UIScrollView的大小。我将所有像素存储在int个变量中。在我填充所有这些文本视图的方法的最后,我正在使用它:

viewHeight += viewPixelsToBeAdded;
viewHeight -= viewPixelsToBeRemoved;
detailsView.frame = CGRectMake(0, 20, 320, viewHeight);
realDetailsView.frame = CGRectMake(0, 20, 320, viewHeight);

viewHeight是常量视图高度,即1475.我正在使用viewPixelsToBeAddedviewPixelsToBeRemoved来保存视图需要调整大小的像素总数。当我使用上面的代码...我的scrollView(detailsView)只是停止滚动。共。我在视图中有我的对象。该视图位于scrollView中。有没有更简单的方法来调整这些东西?当我增加或减少其高度时,为什么我的scrollView会停止滚动?任何帮助将不胜感激。如果需要,我可以发布整个populateLabels方法。无论如何,我觉得我正在走很长的路。

-(void) populateLabels {

NSString *noInfo = (@"No Information Available");
lblCgName.text = _Campground.campground;
NSString *cgLoc = _Campground.street1; 
NSString *cgCity = _Campground.city;
NSString *cgState = _Campground.state1;
NSString *cgCountry = _Campground.country;
NSString *cgZipPostal = _Campground.zipPostal;
cgLoc = [[cgLoc stringByAppendingString:@", "] stringByAppendingString:cgCity];
cgLoc = [[cgLoc stringByAppendingString:@", "] stringByAppendingString:cgState];
cgLoc = [[cgLoc stringByAppendingString:@", "] stringByAppendingString:cgCountry];
cgLoc = [[cgLoc stringByAppendingString:@" "] stringByAppendingString:cgZipPostal];
lblCgLoc.text = cgLoc;
double dRate = [_Campground.regRate1 doubleValue];
double dRate2 = [_Campground.regRate2 doubleValue];
NSString *rate = [[NSString alloc] initWithFormat:@"$%0.2f",dRate];
NSString *rate2 = [[NSString alloc] initWithFormat:@"$%0.2f",dRate2];
if ([rate2 isEqualToString:@"$0.00"]) {
    lblRate.text = rate;
} else {
    rate = [[rate stringByAppendingString:@" - "] stringByAppendingString:rate2];
    lblRate.text = rate;
}
double dPaRate = [_Campground.paRate1 doubleValue];
double dPaRate2 = [_Campground.paRate2 doubleValue];
NSString *paRate = [[NSString alloc] initWithFormat:@"$%0.2f",dPaRate];
NSString *paRate2 = [[NSString alloc] initWithFormat:@"$%0.2f",dPaRate2];
if ([paRate2 isEqualToString:@"$0.00"]) {
    lblPaRate.text = paRate;
} else {
    paRate = [[paRate stringByAppendingString:@" - "] stringByAppendingString:paRate2];
    lblPaRate.text = paRate;
}
lblLocal1.text = _Campground.localPhone1;
lblLocal2.text = _Campground.localPhone2;
lblTollFree.text = _Campground.tollFree;
lblFax.text = _Campground.fax;
lblEmail.text = _Campground.email;
lblWebsite.text = _Campground.website;
NSString *gps = _Campground.latitude;
NSString *longitude = _Campground.longitude;
gps = [[gps stringByAppendingString:@", "] stringByAppendingString:longitude];
lblGps.text = gps;

int viewHeight = 1475;
int textViewDefaultHeight = 128;
int newTextViewHeight = 0;
int highlightsPixelsRemoved = 0;
int highlightsPixelsAdded = 0;
int notesPixelsRemoved = 0;
int notesPixelsAdded = 0;
int directionsPixelsRemoved = 0;
int directionsPixelsAdded = 0;
int rentalsPixelsRemoved = 0;
int rentalsPixelsAdded = 0;
int viewPixelsToBeRemoved = 0;
int viewPixelsToBeAdded = 0;

//----------------------------------------------------------------------------    

txtHighlights.text = _Campground.highlights;
[self fitFrameToContent:txtHighlights];
newTextViewHeight = txtHighlights.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
    highlightsPixelsAdded = newTextViewHeight - textViewDefaultHeight;
    viewPixelsToBeAdded += highlightsPixelsAdded;
    txtTentsHead.frame = CGRectOffset(txtTentsHead.frame, 0, highlightsPixelsAdded);
    txtTents.frame = CGRectOffset(txtTents.frame, 0, highlightsPixelsAdded);
    txtNotesHead.frame = CGRectOffset(txtNotesHead.frame, 0, highlightsPixelsAdded);
    txtNotes.frame = CGRectOffset(txtNotes.frame, 0, highlightsPixelsAdded);
    txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, highlightsPixelsAdded);
    txtDirections.frame = CGRectOffset(txtDirections.frame, 0, highlightsPixelsAdded);
    txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, highlightsPixelsAdded);
    txtRentals.frame = CGRectOffset(txtRentals.frame, 0, highlightsPixelsAdded);


} else if (newTextViewHeight < textViewDefaultHeight) {
    highlightsPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
    viewPixelsToBeRemoved += highlightsPixelsRemoved;
    txtTentsHead.frame = CGRectOffset(txtTentsHead.frame, 0, -highlightsPixelsRemoved);
    txtTents.frame = CGRectOffset(txtTents.frame, 0, -highlightsPixelsRemoved);
    txtNotesHead.frame = CGRectOffset(txtNotesHead.frame, 0, -highlightsPixelsRemoved);
    txtNotes.frame = CGRectOffset(txtNotes.frame, 0, -highlightsPixelsRemoved);
    txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, -highlightsPixelsRemoved);
    txtDirections.frame = CGRectOffset(txtDirections.frame, 0, -highlightsPixelsRemoved);
    txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, -highlightsPixelsRemoved);
    txtRentals.frame = CGRectOffset(txtRentals.frame, 0, -highlightsPixelsRemoved);

}   


//----------------------------------------------------------------------------    

txtTents.text = _Campground.tents;

//----------------------------------------------------------------------------    

txtNotes.text = _Campground.notes;
[self fitFrameToContent:txtNotes];
newTextViewHeight = txtNotes.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
    notesPixelsAdded = newTextViewHeight - textViewDefaultHeight;
    viewPixelsToBeAdded += notesPixelsAdded;

    txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, notesPixelsAdded);
    txtDirections.frame = CGRectOffset(txtDirections.frame, 0, notesPixelsAdded);
    txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, notesPixelsAdded);
    txtRentals.frame = CGRectOffset(txtRentals.frame, 0, notesPixelsAdded);


} else if (newTextViewHeight < textViewDefaultHeight) {
    notesPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
    viewPixelsToBeRemoved += notesPixelsRemoved;

    txtDirectionsHead.frame = CGRectOffset(txtDirectionsHead.frame, 0, -notesPixelsRemoved);
    txtDirections.frame = CGRectOffset(txtDirections.frame, 0, -notesPixelsRemoved);
    txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, -notesPixelsRemoved);
    txtRentals.frame = CGRectOffset(txtRentals.frame, 0, -notesPixelsRemoved);

}   


//----------------------------------------------------------------------------    

txtDirections.text = _Campground.directions;
[self fitFrameToContent:txtDirections];
newTextViewHeight = txtDirections.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
    directionsPixelsAdded = newTextViewHeight - textViewDefaultHeight;
    viewPixelsToBeAdded += directionsPixelsAdded;

    txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, directionsPixelsAdded);
    txtRentals.frame = CGRectOffset(txtRentals.frame, 0, directionsPixelsAdded);


} else if (newTextViewHeight < textViewDefaultHeight) {
    directionsPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
    viewPixelsToBeRemoved += directionsPixelsRemoved;

    txtRentalsHead.frame = CGRectOffset(txtRentalsHead.frame, 0, -directionsPixelsRemoved);
    txtRentals.frame = CGRectOffset(txtRentals.frame, 0, -directionsPixelsRemoved);

} 

//----------------------------------------------------------------------------    

txtRentals.text = _Campground.rentals;
[self fitFrameToContent:txtRentals];
newTextViewHeight = txtRentals.contentSize.height;
if (newTextViewHeight > textViewDefaultHeight) {
    rentalsPixelsAdded = newTextViewHeight - textViewDefaultHeight;
    viewPixelsToBeAdded += rentalsPixelsAdded;


} else if (newTextViewHeight < textViewDefaultHeight) {
    rentalsPixelsRemoved = textViewDefaultHeight - newTextViewHeight;
    viewPixelsToBeRemoved += rentalsPixelsRemoved;

} 

viewHeight += viewPixelsToBeAdded;
viewHeight -= viewPixelsToBeRemoved;
detailsView.frame = CGRectMake(0, 20, 320, viewHeight);
scrollView.frame = CGRectMake(0, 20, 320, viewHeight);
}

2 个答案:

答案 0 :(得分:2)

听起来你在UIView和UIScrollView上都设置了比屏幕大得多的相同帧?因此滚动视图不会滚动,因为它与其中的视图大小相同 - 滚动显示没有额外的内容。

可能您希望单独保留滚动视图框,以便滚动视图在屏幕上占用相同的空间量,而不管其内容的大小,并设置contentSize属性。同样地,我希望你想要将0作为滚动视图中视图的y坐标传递,因为视图框架是相对于它们的父视图。

答案 1 :(得分:1)

scrollView有两种尺寸:它的frame决定了它在屏幕上的大小(以及它在哪里),它的contentSize决定了它的滚动距离。

如果contentSizeframe.size相同或小于contentSize,则scrollView将不会滚动(因为没有可滚动的内容)。

通常,您需要将scrollView的UIImageView设置为您放入其中的视图的大小。

假设您的frame大小为1000x1000点。其CGRectMake(0, 0, 1000, 1000)可能会CGRectMake(0, 0, 320, 480)。假设您的ScrollView是纵向iPhone上的全屏,因此它的框架为contentSize。如果您现在将其CGSizeMake(1000, 1000)设置为contentSize,则可以滚动所有图片。如果您将contentSize设为320 x 480的矩形,则根本不会滚动。如果将{{1}}设置为2000x2000,则可以比图片进一步滚动。