简体   繁体   English

iOS 10问题:即使设置了ContentSize,UIScrollView也不会滚动

[英]iOS 10 Issue: UIScrollView Not Scrolling, Even When ContentSize Is Set

UPDATE: This is an iOS 10 issue. 更新: 这是一个iOS 10问题。 This still works as before in iOS 9. 这仍然像以前一样在iOS 9中运行。

This is ...interesting. 这是有趣的。

I just converted my "teaching project" (a "toy" app) to Swift 3. 我刚刚将我的“教学项目”(一个“玩具”应用程序)转换为Swift 3。

It has been working for a couple of years under Swift 1.2. 它已经在Swift 1.2下工作了几年。

All of a sudden, my UIScrollView is not scrolling, even when I set the contentSize way past its lower boundary. 突然间,即使我将contentSize设置为超出其下边界,我的UIScrollView也不会滚动。

Here's the relevant code (the displayTags routine is called with an array of images that are displayed centered and slightly vertically offset, leading to a vertical chain): 这是相关的代码(displayTags例程使用一组图像调用,这些图像以居中和略微垂直偏移的方式显示,导致垂直链):

    /*******************************************************************************************/
    /**
        \brief  Displays the tags in the scroll view.

        \param inTagImageArray the array of tag images to be displayed.
    */
    func displayTags ( inTagImageArray:[UIImage] )
    {
        self.tagDisplayView!.bounds = self.tagDisplayScroller!.bounds
        if ( inTagImageArray.count > 0 )    // We need to have images to display
        {
            var offset:CGFloat = 0.0    // This will be the vertical offset for each tag.

            for tag in inTagImageArray
            {
                self.displayTag ( inTag: tag, inOffset: &offset )
            }
        }
    }
    /*******************************************************************************************/
    /**
        \brief  Displays a single tag in the scroll view.

        \param inTag a UIImage of the tag to be displayed.
        \param inOffset the vertical offset (from the top of the display view) of the tag to be drawn.
    */
    func displayTag ( inTag:UIImage, inOffset:inout CGFloat )
    {
        let imageView:UIImageView = UIImageView ( image:inTag )
        var containerRect:CGRect = self.tagDisplayView!.frame   // See what we have to work with.
        containerRect.origin = CGPoint.zero
        let targetRect:CGRect = CGRect ( x: (containerRect.size.width - inTag.size.width) / 2.0, y: inOffset, width: inTag.size.width, height: inTag.size.height )
        imageView.frame = targetRect
        containerRect.size.height = max ( (targetRect.origin.y + targetRect.size.height), (containerRect.origin.y + containerRect.size.height) )
        self.tagDisplayView!.frame = containerRect
        self.tagDisplayView!.addSubview ( imageView )
        self.tagDisplayScroller!.contentSize = containerRect.size
        print ( "Tag Container Rect: \(containerRect)" )
        print ( "    Tag ScrollView Bounds: \(self.tagDisplayScroller!.bounds)" )
        inOffset = inOffset + (inTag.size.height * 0.31)
    }

Note that the scrollView's contentSize is expanded each time a tag is added. 请注意,每次添加标记时都会展开scrollView的contentSize。 I checked (see the print statements), and the value seems to be correct. 我检查了(见打印语句),值似乎是正确的。

The project itself is completely open-source. 该项目本身是完全开源的。

This is where this issue manifests (I have other bugs, but I'll get around to fixing them after I nail this one). 这就是这个问题所表现出来的地方 (我有其他的错误,但是在我指出这个问题之后我会解决它们)。

I'm sure that I am doing something obvious and boneheaded (usually the case). 我确信我正在做一些明显和愚蠢的事情(通常就是这种情况)。

Anyone have any ideas? 有人有想法么?

it will work when you will set contentSize on main thread and put this code in - (void)viewDidLayoutSubviews 你将在主线程上设置contentSize并将此代码放入- (void)viewDidLayoutSubviews

- (void)viewDidLayoutSubviews
{
    dispatch_async (dispatch_get_main_queue(), ^
                    {
                        [self.scrollview setContentSize:CGSizeMake(0, 2100)];
                    });
}

contentSize should be change in main thread in swift. contentSize应该在swift中改变主线程。

DispatchQueue.main.async {
        self.scrollView.contentSize = CGSize(width: 2000, height: 2000)
    }

This worked for me. 这对我有用。

OK. 好。 I solved it. 我解决了

I remove every single auto layout constraint for the internal (scrolled) view at build time. 我在构建时删除内部(滚动)视图的每个自动布局约束。

I assume that iOS 10 is finally honoring the contract by forcing the top of the scrolled view to attach to the top of the scroller, even though the user wants to move it. 我假设iOS 10终于通过强制滚动视图的顶部附加到滚动条的顶部来表达合同,即使用户想要移动它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM