iphone - 调整 View 大小。没有按我计划的方式工作

标签 iphone objective-c ios uiview

我正在根据 UITextView 移动的像素数调整 UIView 和 UIScrollView 的大小。我将所有像素都存储在 int 变量中。在填充所有这些 TextView 的方法结束时,我使用了这个:

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

viewHeight 是恒定的 View 高度,即 1475。我使用 viewPixelsToBeAddedviewPixelsToBeRemoved 来保存 View 需要调整大小的像素总数。当我使用上面的代码时...我的 scrollView (detailsView) 只是停止滚动。共。我在一个 View 中有我的对象。该 View 位于 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);
}

最佳答案

听起来您在 UIView 和 UIScrollView 上都设置了相同的框架,它比屏幕大得多?所以 ScrollView 不会滚动,因为它与其中的 View 大小相同 — 滚动不会显示任何额外内容。

可能你想单独留下 ScrollView 框架,这样 ScrollView 在屏幕上占用相同数量的空间而不管其内容的大小,并设置 contentSize属性(property)。同样,我希望您将 0 作为 ScrollView 中 View 的 y 坐标传递,因为 View 框架是相对于其父级的。

关于iphone - 调整 View 大小。没有按我计划的方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8954741/

相关文章:

iphone - 将文件从 iphone 上传到 Web 服务器

iphone - 在 iOS 中使用 objectForKey 解析带有 NSJSONSerialization 类的 json

iphone - 我们需要 iPhone/iPad 来开发它吗?

iphone - UIImagePickerController - 设置最大视频持续时间

ios - 如何在 UITableViewController 中使用表格部分创建分页

ios - 如果我在 Realm 上创建数据库,我的用户是否可以在没有互联网访问权限的情况下访问它?

ios - Swift: fatal error :在解包可选值时意外发现 nil (SpriteKit)

ios - iPhone 上的 iAd 和 Admob 插页式集成

iphone - 一起使用 UINavigationController 和标签栏

ios - 首次加载后如何在 UICollectionView 中选择某些项目?