UITabBarController 中奇怪的界面错误 UIScrollView.其他人可以复制吗? [英] Weird interface bug UIScrollView in UITabBarController. Reproducible by others?

查看:44
本文介绍了UITabBarController 中奇怪的界面错误 UIScrollView.其他人可以复制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UIScrollView 出现了一个奇怪的界面错误,我不知道如何解决.我只写了一行代码(如下所示),这是一个空白项目的设置,很容易重现

I get a weird interface bug with my UIScrollView and I cant figure out how to solve it. I only wrote one line of code (shown below) and it is a blank project's setup easily reproducible!

设置:

我有一个 UIScrollView 包含一个 UISegmentedControl (因为控件是动态加载的,它可能会超过屏幕的宽度,并且 scrollView 应该水平滚动 segmentedControl,scrollview 的高度与 UISegmentedControl 的高度相同).包含它的 ViewController 嵌入在 tabBar(或导航栏,也显示错误)中.整个过程就是使用自动布局.

I have a UIScrollView that contains a UISegmentedControl (since the segments of the control are loaded dynamically, it could exceed the width of the screen and the scrollView is supposed to scroll the segmentedControl horizontally, the height of the scrollview is the same as the UISegmentedControl's). The ViewController that contains this is embedded in a tabBar (or navigation bar, which also shows the bug). The whole thing is using Auto-Layout.

错误:

当我将 SegmentedControl 向右滚动一定程度,然后通过单击 tabBarController 上的另一个选项卡切换视图控制器时,分段控件的内容偏移在切换回初始视图控制器时会奇怪地偏移.当我尝试滚动到滚动视图的最左侧时,它不会让我.切换标签几次后,它会再次修复,我可以重新做一遍.

When I scroll the SegmentedControl some degree to the right and then switch the viewController by clicking the other tab on the tabBarController, the content-offset of the segmented control gets weirdly shifted when switching back to the initial viewcontroller. When I try to scroll to the leftmost part of the scrollview it won't let me. When switching the tabs a couple of times, it gets fixed again and I can do this over.

我做了什么(你能重现这个吗?):

What I did (can you reproduce this?):

  1. 创建一个空白的单视图ios项目
  2. 将已经给定的 viewController 嵌入到 tabbarcontroller 中.
  3. 在视图的上部放置一个滚动视图,从左到右适合屏幕.
  4. 在滚动视图的左上角放置一个 UISegmentedControl 并拖动滚动视图以适合分段控件的高度高度
  5. 稍微更改 Segmented 控件的宽度,以便 xcode 添加宽度约束.在分段控件的宽度约束中,将宽度约束的关系更改为大于或等于"
  6. 为分段控件创建一个出口
  7. 在 viewDidload 中添加此代码

  1. Create a blank single-view ios project
  2. Embed the already given viewController in a tabbarcontroller.
  3. Put a scrollView on the upper portion of the view that fits the screen from left to right.
  4. Put a UISegmentedControl on the topleft corner of the scrollview and drag the scrollview to fit the segmented controls height height
  5. Change the Segmented control's width a bit so xcode adds a width-constraint. in the segmented control's width constraint change the width constraint's relation to "greater than or equal"
  6. create an outlet to the segmented control
  7. in viewDidload add this code

[self.segmentedControl insertSegmentWithTitle:@"A really long title so it you have to scroll to see it" atIndex: 0 animated: NO];

  • 创建一个空白的视图控制器并将其添加为 tabbarController 的第二个视图控制器.

  • Create a blank viewcontroller and add it as a second viewController for the tabbarController.

    这是我的故事板的样子:

    This is how my storyboard looks like:

    现在运行项目,将分段控件滚动到最右边.切换选项卡并切换回来,请告诉我您的滚动视图现在的行为 - 以及原因.

    Now run the project, scroll the segmented control to it's right end as far as it goes. Switch the tab and switch back and please tell me how your scrollview now behaves - and WHY.

    我猜这可能与自动布局有关吗?不知道是什么.

    My guess would be it has something to do with Auto Layout maybe? Can't figure out what though.

    我尝试通过在 viewDidAppear 中设置 scrollView 的 contentSize 或在 viewDidAppear 中更改 scrollView 的内容偏移量或更改框架、这些和其他内容的组合来解决此问题....

    I tried fixing this by setting the scrollView's contentSize in viewDidAppear or changing the content offset of the scrollView in viewDidAppear or changing frames, combination of those and what not....

    额外问题:

    不再需要设置 scrollViews contentSize 属性了吗?为什么它会自动滚动内容?

    Is it no longer neccessary to set the scrollViews contentSize property? Why does it scroll the content automatically?

    推荐答案

    谷歌搜索后,我在另一个 StackOverflow 问题中找到了答案.

    After googeling I found the answer in another StackOverflow question.

    您需要做的是将 scrollview.contentOffset 保存在 viewWillDisappear 上,在 viewDidDisappear 上将其设置为 CGPointZero 并将其设置回 viewDidLayoutSubviews 上的保存状态:

    What you need to do is save the scrollview.contentOffset on viewWillDisappear, set it to CGPointZero on viewDidDisappear and set it back to the saved state on viewDidLayoutSubviews:

    -(void) viewWillDisappear: (BOOL) animated { 
     self.lastContentOffset = self.scrollView.contentOffset;
    [super viewWillDisappear: animated];
    }
    
    -(void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear: animated];
    self.scrollView.contentOffset = CGPointZero;
    }
    - (void)viewDidLayoutSubviews {
    [super viewDidlayoutSubviews];
    self.scrollView.contentOffset = self.lastContentOffset;
    }
    

    这篇关于UITabBarController 中奇怪的界面错误 UIScrollView.其他人可以复制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆