ios - 导航 Controller 中的 UIScrollView

标签 ios cocoa-touch uiscrollview

我正在尝试让 UIScrollView 在我的训练应用程序中工作。滚动本身工作正常,但如果我使用导航 Controller ,SVC 的内容区域会向下移动导航栏高度。

without nav controller with nav controller

青色区域为 UIScrollView 背景。 contentSize 设置正确,只允许左右滑动页面。这就是我将图像添加到 ScrollView 的方式

CGSize scrollViewSize = self.scrollView.frame.size;

UIImage * image = [UIImage imageWithContentsOfFile:[cachesDirectory stringByAppendingFormat:@"/%@", fileName]];

CGFloat imageOriginalWidth = image.size.width;
CGFloat imageOriginalHeight = image.size.height;

CGFloat imageOffsetX = 10;

CGFloat imageDisplayedWidth = scrollViewSize.width - (2 * imageOffsetX);
CGFloat imageDisplayedHeight = imageOriginalHeight / imageOriginalWidth * imageDisplayedWidth;

CGFloat imageOffsetY = (scrollViewSize.height - imageDisplayedHeight) / 2;

CGRect viewFrame = CGRectMake(scrollViewSize.width * (filmIndex) + imageOffsetX,
                              imageOffsetY,
                              imageDisplayedWidth,
                              imageDisplayedHeight);

contentAreaHeight = imageDisplayedHeight;

UIImageView * view = [[UIImageView alloc] initWithFrame:viewFrame];
view.backgroundColor = [UIColor greenColor];
view.image = image;
view.contentMode = UIViewContentModeScaleAspectFit;
view.tag = filmIndex;

[self.scrollView addSubview:view];

这就是我设置内容大小和偏移量的方式

self.scrollView.contentSize = CGSizeMake(scrollViewSize.width * ([self.filmsData count]),
                                         contentAreaHeight);
self.scrollView.contentOffset = CGPointMake(scrollViewSize.width * [self.filmsData count] / 2,
                                            0);

我的代码出了什么问题以及如何在使用导航 Controller 时使图像垂直居中?

最佳答案

您可以在 -viewDidLoad 中创建和添加 View ,但您需要使用自动调整大小或在 -viewWillAppear: 中手动设置框架或其他一些触发的方法在 self.view 调整大小后。

Bounds and Frame size in viewDidLoad

关于ios - 导航 Controller 中的 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18014494/

相关文章:

ios - 由于某种原因,UIBezierPath 轮廓不断出现在 View 周围

swift - 如何使用按钮在 ScrollView Controller 中强制输入 "scroll"?

ios - UserDefaults 未在 TextView 中保留数据

ios - 无法在 Objective-C 中使用 Swift 生成的共享框架

ios/xcode : core data throwing error on NSManagedObjectContext

iphone - 线程间 'communication' 问题

iphone - 使用 initWithNibName : bundle: or via an IBOutlet behave differently 创建的 UIViewController

ios - 我可以使用 NSUserDefaults 保存应用程序的表格单元格值吗?

ios - 带有AutoLayout的UIScrollView内的UITableView动态内容大小高度

iOS swift : Constraints did not update on screen