更新:这是一个 iOS 10 问题。这在 iOS 9 中仍然像以前一样有效。
这……很有趣。
我刚刚将我的“教学项目”(一个“玩具”应用程序)转换为 Swift 3。
它已经在 Swift 1.2 下工作了几年。
突然间,即使我将 contentSize 设置为超过其下边界,我的 UIScrollView 也没有滚动。
这里是相关代码(调用 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 都会扩展。我检查了(查看打印语句),该值似乎是正确的。
The project itself is completely open-source.
This is where this issue manifests (我还有其他错误,但我会在解决这个问题后着手修复它们)。
我确定我正在做一些显而易见且愚蠢的事情(通常是这样)。
有人有什么想法吗?
最佳答案
当您在主线程上设置 contentSize
并将此代码放入 - (void)viewDidLayoutSubviews
时,它将起作用
- (void)viewDidLayoutSubviews
{
dispatch_async (dispatch_get_main_queue(), ^
{
[self.scrollview setContentSize:CGSizeMake(0, 2100)];
});
}
关于iOS 10 问题 : UIScrollView Not Scrolling, 即使设置了 ContentSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39560586/