tallybear
tallybear

Reputation: 417

Resizing a View. Not working the way I planned

I'm resizing a UIView and a UIScrollView based on the number of pixels that UITextViews are being moved. I have all of the pixels being stored in int variables. At the end of my method to populate all of these text views I'm using this:

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

viewHeight is the constant view height which is 1475. I'm using viewPixelsToBeAdded and viewPixelsToBeRemoved to hold the total number of pixels that the view needs to be resized. When I use the above code... my scrollView (detailsView) just stops scrolling. Altogether. I have my objects inside of a view. That view is inside of a scrollView. Is there an easier way to go about resizing this stuff? Why does my scrollView stop scrolling when I increase or decrease its height? Any help would be greatly appreciated. I can post the entire populateLabels method if need be. I kinda feel like I'm taking the long route with it anyway.

-(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);
}

Upvotes: 0

Views: 174

Answers (2)

fzwo
fzwo

Reputation: 9902

A scrollView has two sizes: Its frame, which determines how large (and where) it is on the screen, and its contentSize, which determines how far it will scroll.

If contentSize is the same or smaller than frame.size, the scrollView will not scroll (because there is nothing to scroll to).

Usually, you'll want to set the contentSize of a scrollView to the size of the view(s) that you put into it.

Say you have a UIImageView that has a size of 1000x1000 points. Its frame will probably by CGRectMake(0, 0, 1000, 1000). Say your ScrollView is a fullscreen on an iPhone in portrait orientation, so it has a frame of CGRectMake(0, 0, 320, 480). If you now set its contentSize to CGSizeMake(1000, 1000), it will be able to scroll over all of the picture. If you make the contentSize a rectangle of 320 x 480, it won't scroll at all. And if you set the contentSize to 2000x2000, you will be able to scroll further than the picture.

Upvotes: 1

Tommy
Tommy

Reputation: 100652

It sounds like you're setting the same frame, which is much larger than the screen, on both the UIView and the UIScrollView? So the scroll view doesn't scroll because it's the same size as the view within it — there's nothing extra to show by scrolling.

Probably you want to leave the scroll view frame alone, so that the scroll view takes up the same amount of space on screen regardless of the size of its contents, and set the contentSize property. Similarly I'd expect you to want to pass 0 as the y coordinate for the view within the scrollview since view frames are relative to their parent.

Upvotes: 2

Related Questions