IOS 10 issue: UIScrollView does not scroll even when ContentSize is set

UPDATE: This is a problem with iOS 10. It still works as of iOS 9.

It is interesting.

I just converted my “training project” (the “toy” application) to Swift 3.

He has been working for a couple of years in Swift 1.2.

Suddenly my UIScrollView does not scroll even when I set the contentSize path to its lower bound.

Here is the corresponding code (the displayTags procedure is called with an array of images that are displayed in the center and slightly vertically shifted, which leads to a vertical chain):

    /*******************************************************************************************/
    /**
        \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 scrollView contentSize expands each time a tag is added. I checked (see print instructions) and the value seems to be correct.

.

( , , ).

, - ( ).

- ?

+4
3

OK. .

() .

, iOS 10, , , , .

+3

, contentSize - (void)viewDidLayoutSubviews

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

contentSize must be changed in the main thread in swift.

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

It worked for me.

+7
source

Source: https://habr.com/ru/post/1654989/


All Articles