即使我设置内容大小,UIScrollView也不会滚动
问题描述:
我有一个UIView,它是UIScrollView的子视图。即使我设置内容大小,UIScrollView也不会滚动
我做这个设置内容大小viewDidLoad
(有用的片段我在此网站上发现):
CGFloat scrollViewHeight = 0.0f;
for (UIView* view in self.scrollView.subviews)
{
scrollViewHeight += view.frame.size.height;
}
[self.scrollView setContentSize:(CGSizeMake(320, scrollViewHeight))];
使用NSLogs我已经确定的内容大小高度比的高度大滚动视图。但是,它仍然不会滚动。 (我只对垂直滚动感兴趣)
在IB中启用滚动功能,所以我错过了什么?
答
是userInteractionEnabled属性是否等于TRUE?也许你有一个你的滚动视图的子视图,窃取触摸?
答
我同意Sepehr。别的东西一定是在窃取你的触动。您可以测试此购买,将UIScollViewDelegate添加到您的课程并添加下面的一个事件回调方法。如果当你触摸你的滚动视图,他们不会打电话,那么你知道Sepehr是正确的。
(void)scrollViewDidScroll:(UIScrollView *)scrollView
(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
答
这也发生在我身上 - 似乎当画面从笔尖文件的一些调整大小后 viewDidLoad中甚至viewWillAppear中后会发生加载等。 调用堆栈显示它是resizeWithOldSuperviewSize谁在做这
#0 0x0000f040 in -[ScrollView setContentSize:] at .../ScrollView.m:49
#1 0x00092863 in -[UIScrollView _resizeWithOldSuperviewSize:]()
#2 0x0007357a in -[UIView(Geometry) resizeWithOldSuperviewSize:]()
#3 0x00072178 in __46-[UIView(Geometry) resizeSubviewsWithOldSize:]_block_invoke_0()
#4 0x01ccb5a7 in __NSArrayChunkIterate()
#5 0x01ca303f in __NSArrayEnumerate()
#6 0x01ca2a16 in -[NSArray enumerateObjectsWithOptions:usingBlock:]()
#7 0x0007210f in -[UIView(Geometry) resizeSubviewsWithOldSize:]()
#8 0x0056d14c in -[UIView(AdditionalLayoutSupport) _is_layout]()
#9 0x00076dfe in -[UIView(Hierarchy) layoutSubviews]()
#10 0x0007e92d in -[UIView(CALayerDelegate) layoutSublayersOfLayer:]()
#11 0x010fa6b0 in -[NSObject performSelector:withObject:]()
#12 0x022a5fc0 in -[CALayer layoutSublayers]()
#13 0x0229a33c in CA::Layer::layout_if_needed(CA::Transaction*)()
#14 0x022a5eaf in -[CALayer layoutIfNeeded]()
#15 0x0011d8cd in -[UIViewController window:setupWithInterfaceOrientation:]()
#16 0x000661a6 in -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:isRotating:]()
#17 0x00064cbf in -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:]()
#18 0x00064bd9 in -[UIWindow _setRotatableViewOrientation:duration:force:]()
#19 0x00063e34 in __57-[UIWindow _updateToInterfaceOrientation:duration:force:]_block_invoke_0()
#20 0x00063c6e in -[UIWindow _updateToInterfaceOrientation:duration:force:]()
#21 0x00064a29 in -[UIWindow setAutorotates:forceUpdateInterfaceOrientation:]()
#22 0x00067922 in -[UIWindow setDelegate:]()
#23 0x00111fec in -[UIViewController _tryBecomeRootViewControllerInWindow:]()
#24 0x0005ebc4 in -[UIWindow addRootViewControllerViewIfPossible]()
#25 0x0005edbf in -[UIWindow _setHidden:forced:]()
#26 0x0005ef55 in -[UIWindow _orderFrontWithoutMakingKey]()
#27 0x00067f67 in -[UIWindow makeKeyAndVisible]()
#28 0x0000379a in -[AppDelegate application:didFinishLaunchingWithOptions:] at .../AppDelegate.m:24
我没有找到一个妥善的解决办法还只是设定contentSize后来通过调用performSelector:withObject:afterDelay
-(void)viewDidLoad
{
[self performSelector:@selector(adjustScrollViewContentSize) withObject:nil afterDelay:0.1];
}
-(void)adjustScrollViewContentSize
{
_scrollView.contentSize = CGSizeMake(4000, 4000);
}
答
问题来自自动布局根据约束设置contentSize。由于我的观点很复杂,自动布局不符合我的任务,我通过覆盖setContentView:并忽略了零高度的自动布局setContentSize:消息来解决此问题。
@interface MyView : UIScrollView {}
@end
@implementation MyView
- (void)setContentSize:(CGSize)aSize {
if (aSize.height > 0)
[super setContentSize:aSize];
}
@end
在我发布这个问题几分钟后,发生了这种情况。我还通过删除UIView并将其他子视图(如文本字段,标签等)直接放到UIScrollView上来解决我的问题。 –