简体   繁体   中英

UIScrollView not scroll in top screen of iPhone with iOS7

I've a problem with UIScrollView in iPhone with iOS 7

I created horizontal UIScrollView programmatically, I added some colored button and finally I positioned the scroll in the top of the screen.

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)

I removed the status bar of iOS 7 with info.plist setting "View controller-based status bar appearance = NO"

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. Re-reading the iOS SDK 6.0 release notes for the twentieth time I found that:

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.

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.

- (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? If it is iOS7 and you change to iOS6.1 and get the expected behavior, then it is most probably 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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