问题
I recently upgraded to the latest SDK for iOS5 and am running into issues with a UIScrollView I have setup. The scrollview is dynamically sized depending on how many objects I want to show, and programatically adds objects to it. The issue I'm running into is that it is initializing the scrollview on load that has an offset even though I have the code: [scrollViewObj setContentOffset:CGPointZero];
I also sent the content offset coordinates to console at the end and it does register (0, 0), but for some reason the UI just isn't picking it up.
Was there something new in iOS 5 that makes this stop working?
My code is below:
Code:
-(void)loadKeyParticipants:(ItineraryItem *)itinerary
{
// Reset the scroll view back to initial position
//keyParticipants.contentOffset = CGPointZero;
[keyParticipants setContentOffset:CGPointZero];
// clean up scroll view from previous use
while([[keyParticipants subviews] count] > 0)
[[[keyParticipants subviews] objectAtIndex:0] removeFromSuperview];
// Defined number of frames for key participant scroll view
int numOfFrames = [[itinerary keyParticipantList] count];
keyParticipants.contentSize=CGSizeMake(PARTICIPANT_LIST_X_OFFSET*numOfFrames,208);
int i;
for (i = 0; i < [[itinerary keyParticipantList] count]; i++)
{
KeyParticipant *tempParticipant = [[itinerary keyParticipantList] objectAtIndex:i];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent: [tempParticipant participantPictureFile] ];
// Insert participant picture
// textOffsetFromMissingPic is used to shift the text to the right
// of the picture to left-align to fill up the space of a missing
// picture
UIImage *image1;
int textOffsetFromMissingPic = 0;
if([[tempParticipant participantPictureFile] isEqualToString:@""])
{
// To re-enable shadow image, uncomment first line, and set
// second line to 0
//image1=[UIImage imageNamed: @"blank-profile-md.png"];
textOffsetFromMissingPic = -157;
}
else
{
image1=[[UIImage alloc] initWithContentsOfFile:docFile];
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:image1];
tempImageView.frame=CGRectMake(PARTICIPANT_LIST_X_OFFSET*i,0,152,108);
tempImageView.contentMode=UIViewContentModeScaleAspectFit;
[self.keyParticipants addSubview:tempImageView];
textOffsetFromMissingPic = 0;
}
// Insert participant names/title
UILabel *label1=[[UILabel alloc] initWithFrame:CGRectMake(textOffsetFromMissingPic + PARTICIPANT_LIST_X_OFFSET*i+161, 2, 456, 27)];
// Generate approptiate name header with salutation and title conditionally
// if they are available
NSString *fullParticipantHeading = @"";
if([tempParticipant participantSalutation] != nil)
fullParticipantHeading = [NSString stringWithFormat:@"%@ %@", [tempParticipant participantSalutation], [tempParticipant participantName]];
else
fullParticipantHeading = [tempParticipant participantName];
if([tempParticipant participantTitle] != nil)
fullParticipantHeading = [NSString stringWithFormat:@"%@, %@", fullParticipantHeading, [tempParticipant participantTitle]];
// Populate label for name header
[label1 setText:fullParticipantHeading];
[label1 setBackgroundColor:[UIColor clearColor]];
label1.font = [UIFont fontWithName:@"Helvetica" size: 24.0];
label1.font = [UIFont boldSystemFontOfSize:24.0];
label1.textColor = [UIColor colorWithRed:0 green:0 blue:255 alpha:255];
[self.keyParticipants addSubview:label1];
// Insert contact information
UITextView *textView1=[[UITextView alloc] initWithFrame:CGRectMake(textOffsetFromMissingPic + PARTICIPANT_LIST_X_OFFSET*i+153, 28, 508, 29)];
[textView1 setText:[tempParticipant participantContact]];
[textView1 setBackgroundColor:[UIColor clearColor]];
textView1.font = [UIFont fontWithName:@"Helvetica" size:14.0];
textView1.editable=NO;
textView1.scrollEnabled=NO;
textView1.dataDetectorTypes=UIDataDetectorTypeAll;
[self.keyParticipants addSubview:textView1];
// CHANGED: Participant Role - Company Name
UILabel *meetingNameLabel=[[UILabel alloc] initWithFrame:CGRectMake(textOffsetFromMissingPic + PARTICIPANT_LIST_X_OFFSET*i+161, 51, 503, 27)];
[meetingNameLabel setText:[NSString stringWithFormat:@"%@ - %@", [tempParticipant role], [tempParticipant companyName]]];
[meetingNameLabel setBackgroundColor:[UIColor clearColor]];
meetingNameLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
[self.keyParticipants addSubview:meetingNameLabel];
// Insert meeting time
UILabel *meetingTimeLabel=[[UILabel alloc] initWithFrame:CGRectMake(textOffsetFromMissingPic + PARTICIPANT_LIST_X_OFFSET*i+161, 73, 503, 21)];
[meetingTimeLabel setText:[tempParticipant meetingTime]];
meetingTimeLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
meetingTimeLabel.textColor = [UIColor darkGrayColor];
[self.keyParticipants addSubview:meetingTimeLabel];
// Insert meeting location
UILabel *meetingLocationLabel=[[UILabel alloc] initWithFrame:CGRectMake(textOffsetFromMissingPic + PARTICIPANT_LIST_X_OFFSET*i+161, 90, 503, 21)];
[meetingLocationLabel setText:[tempParticipant meetingLocation]];
meetingLocationLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];
meetingLocationLabel.textColor = [UIColor darkGrayColor];
[self.keyParticipants addSubview:meetingLocationLabel];
// Insert biography
UITextView *bioTextView=[[UITextView alloc] initWithFrame:CGRectMake(PARTICIPANT_LIST_X_OFFSET*i, 111, 663, 97)];
[bioTextView setText:[tempParticipant bioText]];
bioTextView.font = [UIFont fontWithName:@"Helvetica" size:18.0];
bioTextView.editable=NO;
[self.keyParticipants addSubview:bioTextView];
// Insert swipe icon if there's multiple key participants and the page is
// not the last page
if( ([[itinerary keyParticipantList] count] > 1) &&
(i != [[itinerary keyParticipantList] count] - 1))
{
UIImage *swipeImage = [UIImage imageNamed: @"swipeIcon.png"];
UIImageView *swipeImageView = [[UIImageView alloc] initWithImage:swipeImage];
swipeImageView.frame=CGRectMake(PARTICIPANT_LIST_X_OFFSET*i+615,0,40,40);
swipeImageView.contentMode=UIViewContentModeScaleAspectFit;
[self.keyParticipants addSubview:swipeImageView];
}
}
NSLog(@"X: %f / Y: %f", [keyParticipants contentOffset].x, [keyParticipants contentOffset].y);
}
来源:https://stackoverflow.com/questions/7813205/uiscrollview-content-offset-on-load-not-reflected-in-ui-for-ios-5