UIScrollView在iOS11中不滚动

UIScrollView在iOS11中不滚动

问题描述:

我在代码中创建了UIScrollView,它在iOS10中工作。我今天更新了我的Xcode,并且它不再在iOS 11中滚动(模拟器是iOS11并且不工作;物理iPad仍然是iOS10并且工作正常)。UIScrollView在iOS11中不滚动

用户可以在需要时添加子视图。当它的第一个子视图,我将它锚定到滚动视图的左侧,顶部和底部。然后我子视图右侧锚滚动视图右侧这给contentSize它的大小,所以它知道它需要使滚动

UIScrollView *scrollViewMain = [UIScrollView new]; 
scrollViewMain.delegate = self; 
scrollViewMain.backgroundColor = [UIColor greenColor]; 
scrollViewMain.translatesAutoresizingMaskIntoConstraints = NO; 
scrollViewMain.directionalLockEnabled = YES; 
self.scrollViewMain = scrollViewMain; 

... // other code 

if (self.countPlayers == 1) { 
    [self.scrollViewMain addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[playerCardView(400)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(playerCardView)]]; 
    [self.scrollViewMain addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[playerCardView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(playerCardView)]]; 

    NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:self.scrollViewMain attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:playerCardView attribute:NSLayoutAttributeRight multiplier:1 constant:10]; 

    self.constraintScrollViewRight = constraint; 

    [self.scrollViewMain addConstraint:constraint]; 
} 
else { 
    [self.scrollViewMain addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[previousPlayerCardView]-[playerCardView(==previousPlayerCardView)]" options:0 metrics:nil views:@{@"previousPlayerCardView": player.previousPlayer.playerViewCard, @"playerCardView": player.playerViewCard}]]; 

    [self.scrollViewMain addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[playerCardView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(playerCardView)]]; 
} 

if (self.constraintScrollViewRight) { 
    [self.scrollViewMain removeConstraint:self.constraintScrollViewRight]; 
} 

NSLayoutConstraint *constraintRight = [NSLayoutConstraint constraintWithItem:self.scrollViewMain attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:playerCardView attribute:NSLayoutAttributeRight multiplier:1 constant:10]; 
self.constraintScrollViewRight = constraintRight; 
[self.scrollViewMain addConstraint:constraintRight]; 

[self.scrollViewMain layoutIfNeeded]; 
DLog(@"self.scrollViewMain: %@", self.scrollViewMain); 
DLog(@"self.scrollViewMain.contentSize: %@", NSStringFromCGSize(self.scrollViewMain.contentSize)); 

contentSize不会变大,然后滚动视图的框架:

2017-10-04 20:01:58.479446-0500 [ViewController addPlayer]_block_invoke [Line 242] self.scrollViewMain: <UIScrollView: 0x7fa9fd01b000; frame = (286 20; 462 1014); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x60000005ff20>; layer = <CALayer: 0x600000233580>; contentOffset: {0, 0}; contentSize: {826, 89}; adjustedContentInset: {0, 0, 0, 0}> 
2017-10-04 20:01:58.479969-0500 [ViewController addPlayer]_block_invoke [Line 243] self.scrollViewMain.contentSize: {826, 89} 

为什么iOS11会破坏我的代码?

编辑:

而且,我尽量向右滚动,以显示新的子视图出现时:

[self.scrollViewMain scrollRectToVisible:playerCardView.frame animated:YES]; 

它什么都不做。

SOLUTION:

我改变

[self.scrollViewMain addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[playerCardView(200)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(playerCardView)]]; 

[self.scrollViewMain addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollViewMain attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:playerCardView attribute:NSLayoutAttributeLeft multiplier:1 constant:10]]; 

UIScrollView现在有一个左,右锚,可以计算出其contentSize

现在视觉格式部分破坏的一种糟糕的错误。

+0

有没有人提起了错误与苹果在这吗? – Tawpie

尽量不使用NSLayoutConstraint constraintsWithVisualFormat创建所有的约束条件:

我刚刚有完全相同的问题,在我的应用程序在iOS上11,升级到了XCode 9

几个小时后之后试图找出什么约束导致了我的代码中的问题,我碰巧用所有的调用NSLayoutConstraint constraintsWithVisualFormat:与建设约束“的代码”使用NSLayoutConstraint constraintWithItem:,而不是;现在它的工作原理是它应该...

我想苹果已经修改了视觉格式分析器中,导致一些意想不到的副作用的方式...

+0

这是一个重要的重构......任何人都有@Hypo的同样成功?还@Hypo,你重构*所有*你的约束或只是UIScrollView的子视图? – Tawpie

+0

只是UIScrollView的直接子视图。 ..我的猜测是,它只是连接到UIScrollView的内容宽度和/或高度的重要性的重要性...至少,在我看来,似乎... – Hypo

+0

这样做了。我改变了VF约束,它现在滚动。增加的解决方案。我猜它只是'UIScrollView';子视图需要附加到滚动视图的一侧,以便它可以计算'contentSize'和知道它是否需要滚动。 – Padin215

为什么iOS11会破坏我的代码?

因为这是苹果的工作。

+0

哈哈,那是一个快速的回应! – Padin215

+0

:D这是真的。每当有新的iOS出现时,就会出现问题。希望这个有一个实际的解决方案。同时,抱歉滥用答案部分。这里有同样的问题。 – GeneCode

+0

同样的问题。我也有一个“无限循环”来计算'content size',这会在添加布局约束时让我的应用程序冻结。 iOS 11.1测试版中的问题仍然存在。 :( –