Rectangle 27 2

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

Doesn't mean there's no way to make it work. But probably means you want to limit the amount of time you spend banging your head against the wall trying to get it to work.

That said, I have successfully embedded UIWebViews in UIScrollViews as long as one's userInteractionEnabled was set to NO.

oh ... oh ... my god. i don't know why i implemented a scroll view. you're totally right. a webview scrolls itself. ah now i know. it was a normal view before i switched to a webview. thanks! dropped the scrollview, everything works fine.

iphone - UIScrollView not bouncing vertical when scrolled to top - Sta...

iphone objective-c uiwebview uiscrollview bounce
Rectangle 27 390

for (id subview in webView.subviews)
  if ([[subview class] isSubclassOfClass: [UIScrollView class]])
    ((UIScrollView *)subview).bounces = NO;

It'll be accepted to App Store as well.

Update: in iOS 5.x+ there's an easier way - UIWebView has scrollView property, so your code can look like this:

webView.scrollView.bounces = NO;

It works. No subclassing required and if Apple ever change the UIWebView hierarchy, it shouldn't crash.

This fix seems to only work in my OS 4.1 Simulator, not on my OS 3.1.3 iPhone. Does anybody know if this fix only works on particular OS versions?

@MirukRusin - How can you guarantee that this app will be accepted based on three lines of code? :P

@Moshe because it's using public api and I'm using it in oscpad.com app

UIWebView
[webView setBounces:NO]

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

iphone - Stop UIWebView from "bouncing" vertically? - Stack Overflow

iphone objective-c uiwebview
Rectangle 27 5

I didn't test this particular situation, but I've managed to prevent a scrollview from bouncing at the top and bottom by subclassing the scrollview and overriding setContentOffset: and setContentOffset:animated:. The scrollview calls this at every scroll movement, so I'm fairly certain they will be called when scrolling to the textfield.

You can use the delegate method textFieldDidBeginEditing: to determine when the scroll is allowed.

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.blockingTextViewScroll = YES;
}

-(void)setContentOffset:(CGPoint)contentOffset
{
    if(self.blockingTextViewScroll)
    {
        self.blockingTextViewScroll = NO;
    }
    else
    {
        [super setContentOffset:contentOffset];
    }
}


-(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
{
    if(self.blockingTextViewScroll)
    {
        self.blockingTextViewScroll = NO;
    }
    else
    {
        [super setContentOffset:contentOffset animated:animated];
    }
}

If your current scroll behaviour works with a setContentOffset: override, just place it inside the else blocks (or preferably, in a method you call from the else blocks).

iphone - Can I override scroll view's automatic behavior to scroll to ...

iphone uiscrollview first-responder
Rectangle 27 18

UIView *firstView = [webView.subviews firstObject];

if ([firstView isKindOfClass:[UIScrollView class]]) {

    UIScrollView *scroll = (UIScrollView*)firstView;
   [scroll setScrollEnabled:NO];  //to stop scrolling completely
   [scroll setBounces:NO]; //to stop bouncing 

}

Dont do that - my app got rejected for using the first line. Wasted so much time for me. i used [[webView.subviews lastObject] setScrollEnabled:NO]; and apple said it was a private API and hence rejected it; am about to resubmit now but used "userInteractionEnabled:NO" since i dont need any active links in the app.

UIWebView inherits from UIView. UIView has property subviews. What is private here? All of this you can find on developer.apple.com

I have something like 4 apps on the appstore using this. You app was clearly rejected for another reason. this is not a private API.

This is really not a good idea. You're relying on the fact that the UIScrollView is the first subview of UIWebView which may well be the case for now but this could easily change in future (even minor) updates to iOS or particular configurations. You should really do a for iteration over subviews to actually find the UIScrollView (it really isn't that much work and at least you are guaranteed to get a UIScrollView and not crash your program...

iphone - Stop UIWebView from "bouncing" vertically? - Stack Overflow

iphone objective-c uiwebview
Rectangle 27 16

I updated the content size to disable vertical scrolling, and the ability to scroll still remained. Then I figured out that I needed to disable vertical bounce too, to disable completly the scroll.

Maybe there are people with this problem too.

You saved me. My contentSize and frame were the same and I could not for the life of me figure out what was wrong. Thanks a bunch!!

Your answer is why I love stackoverflow! :-) Holy smoke... this drove me nuts! You are right. It's the bounce effect. I want the bounce effect, but not when there is no need to scroll, so I'll be looking to disable/re-enable that effect now in code depending on whether there is a need to scroll.

Okay, very easy to enable/disable the bounces on a UIScrollView at runtime. In my case I only care about the height of the scroll content and this did the trick: self.scrollView.bounces = (BOOL)(self.scrollView.contentSize.height > self.scrollView.frame.size.height);

iphone - Disabling vertical scrolling in UIScrollView - Stack Overflow

iphone ios uiscrollview
Rectangle 27 16

I updated the content size to disable vertical scrolling, and the ability to scroll still remained. Then I figured out that I needed to disable vertical bounce too, to disable completly the scroll.

Maybe there are people with this problem too.

You saved me. My contentSize and frame were the same and I could not for the life of me figure out what was wrong. Thanks a bunch!!

Your answer is why I love stackoverflow! :-) Holy smoke... this drove me nuts! You are right. It's the bounce effect. I want the bounce effect, but not when there is no need to scroll, so I'll be looking to disable/re-enable that effect now in code depending on whether there is a need to scroll.

Okay, very easy to enable/disable the bounces on a UIScrollView at runtime. In my case I only care about the height of the scroll content and this did the trick: self.scrollView.bounces = (BOOL)(self.scrollView.contentSize.height > self.scrollView.frame.size.height);

iphone - Disabling vertical scrolling in UIScrollView - Stack Overflow

iphone ios uiscrollview
Rectangle 27 2

I was annoyed to find out that UIWebView is not a scroll view, so I made a custom subclass to get at the web view's scroll view. This suclass contains a scroll view so you can customize the behavior of your web view. The punchlines of this class are:

@class CustomWebView : UIWebview
...

- (id) initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
// WebViews are subclass of NSObject and not UIScrollView and therefore don't allow customization.
// However, a UIWebView is a UIScrollViewDelegate, so it must CONTAIN a ScrollView somewhere.
// To use a web view like a scroll view, let's traverse the view hierarchy to find the scroll view inside the web view.
for (UIView* v in self.subviews){
    if ([v isKindOfClass:[UIScrollView class]]){
        _scrollView = (UIScrollView*)v; 
        break;
    }
}
return self;

Then, when you create a custom web view, you can disable bouncing with:

customWebView.scrollView.bounces = NO; //(or customWebView.scrollView.alwaysBounceVertically = NO)

This is a great general purpose way to make a web view with customizable scrolling behavior. There are just a few things to watch out for:

  • as with any view, you'll also need to override -(id)initWithCoder: if you use it in Interface Builder
  • when you initially create a web view, its content size is always the same as the size of the view's frame. After you scroll the web, the content size represents the size of the actual web contents inside the view. To get around this, I did something hacky - calling -setContentOffset:CGPointMake(0,1)animated:YES to force an unnoticeable change that will set the proper content size of the web view.

iphone - Stop UIWebView from "bouncing" vertically? - Stack Overflow

iphone objective-c uiwebview
Rectangle 27 1

One of the subviews of UIWebView should be a UIScrollView. Set its scrollEnabled property to NO and the web view will have scrolling disabled entirely.

Note: this is technically using a private API and thus your app could be rejected or crash in future OS releases. Use @try and respondsToSelector

I tried this out, and it faults when I try it... Strangely, i can access and set scrollView.bounces (with no effect), but when I try and set scrollEnabled, it faults...

I believe the subview is called UIScroller, not UIScrollView. UIScroller is an undocumented and unsupported class that shares a lot in common with UIScrollView, but you can't depend on everything working the same way and not changing in future OS versions.

I did this on an iPad (not iPhone) app running iOS 3.2, and was able to get a UIScrollView out of my UIWebView. I successfully customized this view to change more than just its bouncing behavior so I can testify to this working on iPad. My app did not go through the app store, but I don't think there will be a problem with undocumented API's here. The clever part for this solution is that you're just traversing a UIView hierarchy and using a UIScrollView - both of which are perfectly kosher.

iphone - Stop UIWebView from "bouncing" vertically? - Stack Overflow

iphone objective-c uiwebview
Rectangle 27 271

[self.scrollView setContentOffset:
    CGPointMake(0, -self.scrollView.contentInset.top) animated:YES];
[self.scrollView setContentOffset:CGPointZero animated:YES];

or if you want to preserve the horizontal scroll position and just reset the vertical position:

UIScrollView
contentInset
[self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x, -self.scrollView.contentInset.top) animated:YES];
[scrollView setContentOffset:CGPointMake(0, -scrollView.contentInset.top) animated:YES];

This didn't work for me, probably because I'm doing something crazy with an inverted view in sprite kit. But if anyone else is as crazy as me - and your scrollview is at the bottom by default - this will scroll it to the top: [scrollView setContentOffset:CGPointMake(0, scrollView.contentSize.height-scrollView.frame.size.height) animated:YES];

Late-in-the-day comment: you should adjust for the content inset regardless of OS version. iOS 7 happens to use it for allowing scroll content below the status bar but it's always been exposed as a property, so always been potentially used by somebody.

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

iphone - Get UIScrollView to scroll to the top - Stack Overflow

iphone objective-c ios ipad uiscrollview
Rectangle 27 1

It looks to me like the UIWebView has a UIScrollView. You can use documented APIs for this, but bouncing is set for both directions, not individually. This is in the API docs. UIScrollView has a bounce property, so something like this works (don't know if there's more than one scrollview):

NSArray *subviews = myWebView.subviews;
NSObject *obj = nil;
int i = 0;
for (; i < subviews.count ; i++)
{
    obj = [subviews objectAtIndex:i];

    if([[obj class] isSubclassOfClass:[UIScrollView class]] == YES)
    {
        ((UIScrollView*)obj).bounces = NO;
    }
}

iphone - Stop UIWebView from "bouncing" vertically? - Stack Overflow

iphone objective-c uiwebview
Rectangle 27 31

Here is a Swift 2 & 3 extension that makes it easy:

extension UIScrollView {
    func scrollToTop() {
        let desiredOffset = CGPoint(x: 0, y: -contentInset.top)
        setContentOffset(desiredOffset, animated: true)
   }
}
myScrollView.scrollToTop()

iphone - Get UIScrollView to scroll to the top - Stack Overflow

iphone objective-c ios ipad uiscrollview
Rectangle 27 6

I think this might be a bug in iOS autolayout wrt UIScrollViews. I was having a similar problem only in my case, I would push to a detail view, then pop back to the master view which is a UIScrollView. Content in the scroll view would be shifted up. Scrolling the scroll view to the top, then pushing and popping again and things would be properly reset.

I even tried plain out of the box master detail app using a UIScrollView instead of a table view as master, and was able to demonstrate the defect.

Table views and Collection views do not seem to have this problem.

iphone - iOS AutoLayout issue with ScrollView - Stack Overflow

iphone ios uiscrollview modalviewcontroller autolayout
Rectangle 27 20

setContentOffset:animated:
[scrollView setContentOffset:CGPointZero animated:YES];

iphone - Get UIScrollView to scroll to the top - Stack Overflow

iphone objective-c ios ipad uiscrollview
Rectangle 27 10

Answer for Swift 2.0 and iOS 7+:

let desiredOffset = CGPoint(x: 0, y: -self.scrollView.contentInset.top)
self.scrollView.setContentOffset(desiredOffset, animated: true)

Update 10/6/16: looks like this is still valid in Swift 3.0

Thanks for using variables

iphone - Get UIScrollView to scroll to the top - Stack Overflow

iphone objective-c ios ipad uiscrollview
Rectangle 27 4

For iOS 11 and above - work with the new adjustedContentInset

if #available(iOS 11.0, *) {
    let offset = CGPoint(x: -scrollView.adjustedContentInset.left, y: -scrollView.adjustedContentInset.top)
    scrollView.setContentOffset(offset, animated: true)

} else {
    let offset = CGPoint(x: -scrollView.contentInset.left, y: -scrollView.contentInset.top)
    scrollView.setContentOffset(offset, animated: true)
}
prefersLargeTitles
Safe Area

iphone - Get UIScrollView to scroll to the top - Stack Overflow

iphone objective-c ios ipad uiscrollview
Rectangle 27 3

Thats just dumb from Apple. One more weird behaviour to worry about. Anyhow, I ended up setting the scroll view content inset for top to -20 (from IB).

I've observed that -20 to [other object edge] constraints can often fix these odd UI offset behaviors.

iphone - iOS7 UIScrollView show offset content below status bar - Stac...

iphone uiscrollview ios7 status
Rectangle 27 6

In iOS7 I had trouble getting a particular scrollview to go to the top, whihc worked in iOS6, and used this to set the scrollview to go to the top.

[self.myScroller scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

I have the same issue in iOS7. Does anyone know how to enable this for a UITableView?

It's funny how this works when animated is NO, but not when animated is YES. If YES, then it scrolls almost to the topdevgm16

iphone - Get UIScrollView to scroll to the top - Stack Overflow

iphone objective-c ios ipad uiscrollview
Rectangle 27 24

viewController
automaticallyAdjustsScrollViewInsets
NO
YES
self.automaticallyAdjustsScrollViewInsets = false; 
scroller.contentInset = UIEdgeInsetsZero;
scroller.scrollIndicatorInsets = UIEdgeInsetsZero;
scroller.contentOffset = CGPointMake(0.0, 0.0);

2... does not scroll to the bottom

To make it scroll try with large number in contentSize like CGSizeMake(320, 1687). If it works that means you are not setting the contentSize large enough to have all its content.

ios - Why is UIScrollView leaving space on top and does not scroll to ...

ios iphone objective-c uiscrollview
Rectangle 27 24

viewController
automaticallyAdjustsScrollViewInsets
NO
YES
self.automaticallyAdjustsScrollViewInsets = false; 
scroller.contentInset = UIEdgeInsetsZero;
scroller.scrollIndicatorInsets = UIEdgeInsetsZero;
scroller.contentOffset = CGPointMake(0.0, 0.0);

2... does not scroll to the bottom

To make it scroll try with large number in contentSize like CGSizeMake(320, 1687). If it works that means you are not setting the contentSize large enough to have all its content.

ios - Why is UIScrollView leaving space on top and does not scroll to ...

ios iphone objective-c uiscrollview
Rectangle 27 22

self.automaticallyAdjustsScrollViewInsets = false

scrollView.contentInset = UIEdgeInsets.zero

scrollView.scrollIndicatorInsets = UIEdgeInsets.zero;

ios - Why is UIScrollView leaving space on top and does not scroll to ...

ios iphone objective-c uiscrollview