iOS 10问题:即使设置了ContentSize,UIScrollView也不滚动
更新:这是一个iOS 10的问题。 这在iOS 9中仍然可以正常工作。
这很有趣。
我只是把我的“教学项目”(一个“玩具”应用)转换成Swift 3。
它已经在Swift 1.2下工作了几年了。
突然间,我的UIScrollView不滚动,即使当我设置contentSize的方式超过其下限。
这里是相关的代码(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) }
请注意,scrollView的contentSize在每次添加标签时都会展开。 我检查了(看打印声明),这个值似乎是正确的。
该项目本身是完全开源的。
这是这个问题出现的地方 (我有其他的错误,但是我会在钉上这个之后解决它们)。
我敢肯定,我正在做一些明显的,头脑简单的事情(通常是这种情况)。
有人有主意吗?
它将工作,当你将主线程上设置contentSize
并把这个代码在- (void)viewDidLayoutSubviews
- (void)viewDidLayoutSubviews { dispatch_async (dispatch_get_main_queue(), ^ { [self.scrollview setContentSize:CGSizeMake(0, 2100)]; }); }
contentSize在swift中应该在主线程中更改。
DispatchQueue.main.async { self.scrollView.contentSize = CGSize(width: 2000, height: 2000) }
这对我有效。
好。 我解决了它。
在构build时删除内部(滚动)视图的每个自动布局约束。
我认为,即使用户想要移动它,iOS 10终于通过强制滚动视图的顶部附着到滚动条的顶部来履行合同。