Изменение размера представления. Не работает так, как я планировал - PullRequest
0 голосов
/ 21 января 2012

Я изменяю размеры UIView и UIScrollView в зависимости от количества пикселей, которые перемещаются UITextViews.У меня все пиксели хранятся в int переменных.В конце моего метода для заполнения всех этих текстовых представлений я использую это:

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

viewHeight - это постоянная высота представления, равная 1475. Я использую viewPixelsToBeAdded и viewPixelsToBeRemoved дляудерживайте общее количество пикселей, размеры которых необходимо изменить.Когда я использую приведенный выше код ... мой 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 ]

2 голосов
/ 21 января 2012

Похоже, вы устанавливаете один и тот же кадр, который намного больше экрана, как в UIView, так и в UIScrollView? Таким образом, представление прокрутки не прокручивается, потому что оно того же размера, что и представление внутри него - при прокрутке ничего лишнего не видно.

Возможно, вы хотите оставить кадр просмотра с прокруткой в ​​одиночку, чтобы представление с прокруткой занимало одинаковое количество места на экране независимо от размера его содержимого, и установите свойство contentSize. Точно так же я ожидаю, что вы захотите передать 0 в качестве координаты y для представления в представлении прокрутки, так как рамки представления относятся к их родителю.

1 голос
/ 23 января 2012

ScrollView имеет два размера: его frame, который определяет, насколько велик (и где) он находится на экране, и его contentSize, который определяет, как далеко он будет прокручиваться.

Если contentSize равно или меньше, чем frame.size, scrollView не будет прокручиваться (потому что прокручивать нечего).

Обычно вам нужно установить contentSize для scrollView равным размеру представлений, которые вы в него вставили.

Скажем, у вас есть UIImageView, который имеет размер 1000x1000 очков. Его frame, вероятно, будет CGRectMake(0, 0, 1000, 1000). Скажем, ваш ScrollView - это полноэкранный режим на iPhone в портретной ориентации, поэтому он имеет рамку CGRectMake(0, 0, 320, 480). Если вы сейчас установите contentSize на CGSizeMake(1000, 1000), он сможет прокручивать всю картинку. Если вы сделаете contentSize прямоугольником 320 x 480, он вообще не будет прокручиваться. И если вы установите contentSize на 2000x2000, вы сможете прокрутить дальше, чем изображение.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...