菜鸟教程小白 发表于 2022-12-12 13:15:25

ios - 导航 Controller 中的 UIScrollView


                                            <p><p>我正在尝试让 UIScrollView 在我的培训应用程序中工作。滚动本身可以正常工作,但如果我使用的是导航 Controller ,则 SVC 的内容区域会向下移动以达到导航栏高度的数量。</p>

<p> <img src="/image/UXuLW.png" alt="without nav controller"/>
<img src="/image/LyeNa.png" alt="with nav controller"/> </p>

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

<pre><code>CGSize scrollViewSize = self.scrollView.frame.size;

UIImage * image = ];

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 = [ initWithFrame:viewFrame];
view.backgroundColor = ;
view.image = image;
view.contentMode = UIViewContentModeScaleAspectFit;
view.tag = filmIndex;

;
</code></pre>

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

<pre><code>self.scrollView.contentSize = CGSizeMake(scrollViewSize.width * (),
                                       contentAreaHeight);
self.scrollView.contentOffset = CGPointMake(scrollViewSize.width * / 2,
                                          0);
</code></pre>

<p>我的代码有什么问题,使用导航 Controller 时如何使图像垂直居中?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以在 <code>-viewDidLoad</code> 中创建和添加 View ,但您需要使用自动调整大小或在 <code>-viewWillAppear:</code> 中手动设置框架或其他一些触发方法self.view 调整大小后。</p>

<p> <a href="https://stackoverflow.com/questions/12680377/bounds-and-frame-size-in-viewdidload/12683186" rel="noreferrer noopener nofollow">Bounds and Frame size in viewDidLoad</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 导航 Controller 中的 UIScrollView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/18014494/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/18014494/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 导航 Controller 中的 UIScrollView