OGeek|极客世界-中国程序员成长平台

标题: ios - 导航 Controller 中的 UIScrollView [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:15
标题: ios - 导航 Controller 中的 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 时如何使图像垂直居中?



Best Answer-推荐答案


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

Bounds and Frame size in viewDidLoad

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






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4