How to stop iOS app from scrolling up - ios

I made a Content View inside Scroll View added constraints using common Autolayout with UIScrollView tutorials. But how do I stop my Scrollview from scrolling up when there is no content, the view slides down to content, but I want to disable scrolling up, I searched for the answer but there is none to be found, please help.

The ability to scroll slightly beyond the content in a UIScrollView is referred to as "bouncing".
The scroll view must know the size of the content view so it knows when to stop scrolling; by default, it “bounces” back when scrolling exceeds the bounds of the content.
You need to set bounces to no:
If the value of [bounces] is YES, the scroll view bounces when it encounters a boundary of the content. Bouncing visually indicates that scrolling has reached an edge of the content. If the value is NO, scrolling stops immediately at the content boundary without bouncing. The default value is YES
scrollView.bounces = NO;

Related

Voice over accessibility paging problems for scroll view with content inset

Consider the following scenario: A UIScrollView with content inset of top: 500. In this area there is a subview.
When "three finger swiping" to go "to the next page" of the scroll view, the UIScrollView insists that it should never scroll above it's content offset 0. Sometimes it actually scrolls there and says (Page ZERO of 3), but after a while it never jumps there. Is there any way of also including the "content inset" area in the page scrolling?
Here is some test code that replicates this behaviour. Our actual user case is more complex but this is the basic idea:
https://gist.github.com/ullstrm/45bd68032fe92ba13476b506ed70f424
The View Controller has just a single scroll view with all edges constrained to superview with constant 0.
Any ideas of how to let iOS know that it should scroll to this area as well?
You can override Voiceover's scrolling behavior using the accessibilityScroll method. You'll need to manually change UIScrollView's contentOffset, but otherwise it should work just fine.

How to prevent a non-scrollable UIScrollView from stealing scroll event?

To elaborate on my question, I've three scrollviews one top of another, let say base, middle and top. Base scroll view's content size is more so it will scroll, middle scrollview's content size is equal to its frame so it will not scroll, top scrollview's content size is more than its frame so it will also scroll.
Now when I scroll the top scrollview, once it reaches its end, I"m expecting it would scroll the base scroll view as middle is not scrollable. But it seems middle scroll view consumes all my scroll events and not letting the scroll event to go to base scroll view.
I"ve tried by setting more content size to middle scrollview, then it working as expected, first top scroll view scrolls fully then it passes the scroll event to middle scroll view then to the base scroll view.
Now it is more evident that middle scrollview (which is non scrollable) is consuming all of my scrolling event from going to base scroll view. How do I prevent that.
Note : As I've to support zooming, I need scroll view in all three levels.
Any help would be really appreciated.
Thanks.

iOS 7.1 Why is my UIScrollView scrolling away on showing keyboard?

I have a UIScrollView designed with IB.
UIView
UIScrollView
UIView
UITextField
UIButton
...
If I tap on the text field the view scrolls away towards the upper left corner of the screen before the keyboard is appearing. The space above the keyboards remains empty. I can scroll back the view if I drag in this empty space.
I have googled around, but found only postings where users want to scroll UIScrollView. I want the view to stay where it is.
Thanks in advance for any suggestions.
Here's what happened.
You designed the whole thing in Interface Builder.
The scroll view was not scrolling, so you set its contentSize in code to the size of the scroll view's primary subview (what I like to call the content view).
The scroll view was still not scrolling, so you munged the content insets - and this caused the problem that brought you here.
Your mistake was (3). Instead, you should have thought more about (2), i.e., why isn't my scroll view scrolling even though I have given it a nice big content size?
The answer is that, in a storyboard or xib that has auto layout turned on, that's not what you do. What you do is use constraints from the content view to its superview (the scroll view), on all four sides. Set the constant for all four constraints to zero. This causes the content size to match the size of the content view, automatically.

How do I prevent a scroll view from letting a user (temporarily) scroll a UIScrollView out of it's content area?

UIScroll view lets me declared a scrollable area when I have too much content for one page. Strangely, the scroll view in question behaves as desired in the X-axis, with no scrolling whatsoever allowed. Unfortunately, the Y axis -- where scrolling is necessary -- doesn't 'clip' the allowed scroll area to the content size. The user can scroll outside of the content size, and only after they let go does scroll view 'bounce' back to the allowed zone.
I want to prevent the user from scrolling further up than there is content to view (down doesn't bother me) because it looks 'wrong' to have the header at the top of the scroll view pull down, leaving the regular background behind it.
If you are making your UIScrollView in interface this is as simple as deselecting the
"Bounces, Bounces Horizontally, and Bounces Vertically"
check boxes in your scrollView's attributes. If you are designing the UIScrollView in code you can add this.
self.textView.alwaysBounceHorizontal = NO;
self.textView.alwaysBounceVertical = NO;
self.textView.bounces = NO;

UIScrollView scrolling to bottom when UITextField text changes

As it is set up now, I have two UITextViews inside of a UIScrollView. The point of this is that the UITextViews themselves don't scroll, they just get larger (their contentsize) as more text is added. The UIScrollView handles all of the scrolling up and down of the view regardless of how much text there is (think similar to Mail.app where the subject view is above the message view, etc). That said, I've ran into a problem now. When I programmatically add text to my UITextView (in this code, bodyText), the UIScrollView automatically scrolls to the bottom for some reason.
To add the text I just do:
NSRange selectedRange = [bodyText selectedRange];
NSString *selectedText = [bodyText.text substringWithRange:selectedRange];
bodyText.text = [bodyText.text stringByReplacingCharactersInRange:selectedRange withString:[NSString stringWithFormat:#"<b>%#</b>", selectedText]];
So, for instance, if I had 2000 pixels vertically of text and was currently scrolled to position 400 and then added text somewhere around there, the UIScrollView would then go all the way down to 2000.
I've tried to stop it via subclassing UISCrollView and overriding -setContentOffset, but that freezes all scrolling then.
My question is, why does it scroll all the way to the bottom in the first place? The text is added when the user clicks a button somewhere else on the screen, and the scrolling has nothing to do with any finger drags or anything.
In Scroll View Programming Guide you get:
Making a rectangle visible
It is also possible to scroll a
rectangular area so that it is
visible. This is especially useful
when an application needs to display a
control that is currently outside the
visible area into the visible view.
The scrollRectToVisible:animated:
method scrolls the specified rectangle
so that it is just visible inside the
scroll view. If the animated parameter
is YES, the rectangle is scrolled into
view at a constant pace. As with
setContentOffset:animated:, if
animation is disabled, the delegate is
sent a single scrollViewDidScroll:
message. If animation is enabled, the
delegate is sent a series of
scrollViewDidScroll: messages as
animation progresses. In the case of
scrollRectToVisible:animated: the
scroll view’s tracking and dragging
properties are also NO.
If animation is enabled for
scrollRectToVisible:animated:, the
delegate receives a
scrollViewDidEndScrollingAnimation:
message, providing notification that
the scroll view has arrived at the
specified location and animation is
complete.
So maybe you can use this functionality to lock the visibility on your UITextView.

Resources