简体   繁体   English

UIScrollView无法在iOS7的iPhone顶部屏幕中滚动

[英]UIScrollView not scroll in top screen of iPhone with iOS7

I've a problem with UIScrollView in iPhone with iOS 7 我在使用iOS 7的iPhone中使用UIScrollView时出现问题

I created horizontal UIScrollView programmatically, I added some colored button and finally I positioned the scroll in the top of the screen. 我以编程方式创建了水平UIScrollView,我添加了一些彩色按钮,最后我将滚动定位在屏幕顶部。

The scroll is a custom scroll, so I can handle 滚动是一个自定义滚动,所以我可以处理

- (BOOL)touchesShouldCancelInContentView:(UIView *)view {
  return YES;
}

and

scroll.canCancelContentTouches = YES;
scroll.delaysContentTouches = YES;

to allow the scroll to move, even if I press a button 即使按下按钮,也允许滚动移动

Everything perfect except the top of the screen, where the scroll doesn't move. 一切都很完美,除了屏幕顶部,滚动不动。 But I can press the buttons. 但我可以按下按钮。 If I position down the scroll (circa 30px) I can scroll it in its upper part (content size and frame are correct) 如果我放下滚动条(大约30px)我可以在它的上部滚动它(内容大小和框架是正确的)

I removed the status bar of iOS 7 with info.plist setting "View controller-based status bar appearance = NO" 我使用info.plist设置“查看基于控制器的状态栏外观= NO”删除了iOS 7的状态栏

Seems that in the area of status bar I cannot drag the scroll. 似乎在状态栏区域我无法拖动滚动。

Any suggestions? 有什么建议么?

在此输入图像描述

EDIT: Interestingly, if before scrolling, you pull down the notifications tab, then the scroll works even in the upper part. 编辑:有趣的是,如果在滚动之前,您下拉了通知选项卡,那么滚动甚至在上部也可以工作。 When notification tab move up, problem return. 当通知选项卡向上移动时,问题返回。 As if the first touch was managed by the tab and not passes it to the scroll 好像第一次触摸是由选项卡管理而不是将其传递给滚动

Yeah, something strange happened with UIScrollView in pure autolayout environment. 是的,UIScrollView在纯自动布局环境中发生了一些奇怪的事情。 Re-reading the iOS SDK 6.0 release notes for the twentieth time I found that: 第二十次重新阅读iOS SDK 6.0发行说明我发现:

Note that you can make a subview of the scroll view appear to float (not scroll) over the other scrolling content by creating constraints between the view and a view outside the scroll view's subtree, such as the scroll view's superview. 请注意,通过在视图和滚动视图子树外部的视图(例如滚动视图的超级视图)之间创建约束,可以使滚动视图的子视图显示为浮动(不滚动)在其他滚动内容上。

Solution

Connect your subview to the outer view. 将子视图连接到外部视图。 In another words, to the view in which scrollview is embedded. 换句话说,对于嵌入了scrollview的视图。

As IB does not allow us set up constraints between the imageView and a view outside the scroll view's subtree, such as the scroll view's superview then I've done it in code. 由于IB不允许我们在imageView和滚动视图的子树外部的视图之间设置约束,例如滚动视图的超视图,然后我在代码中完成了它。

- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    [self.view removeConstraints:[self.view constraints]];
    [self.scrollView removeConstraints:[self.scrollView constraints]];
    [self.imageView removeConstraints:[self.imageView constraints]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_scrollView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_scrollView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_imageView(700)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imageView(1500)]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_imageView)]];
}

And ! 而且! It works as Charm!! 它充当魅力!

Is your target iOS7 or earlier version? 您的目标是iOS7还是早期版本? If it is iOS7 and you change to iOS6.1 and get the expected behavior, then it is most probably bug. 如果它是iOS7并且您更改为iOS6.1并获得预期的行为,则很可能是bug。

Another approach is also to check the auto layout constraints as already suggested from colleague and of course to watch the contentFrame property in connection with it. 另一种方法是检查同事已经建议的自动布局约束,当然还要观察与之相关的contentFrame属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM