UIScrollView scrolling to bottom when UITextField text changes - ios

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.

Related

UIKit collection view, strange behavior (accessing/scrolling cells)

So I recently implemented a collection view in my app, and I got a bug that I can't seem to solve, searched it and saw no threads about it.
If I have my cursor/finger over the cells i can't scroll through my collection view i need select a "empty" area to scroll.
Second strange Behavior I came across is that I can't directly touch a cell. I need some sort of swipe gesture over it to trigger the code when a cell is selected.
If I go to my collection view on my storyboard and select Delays Content Touches and Cancellable Content Touches in the scrollview section, the collection view scrolls just fine but if I put my finger/cursor over a cell with these option enabled I can't access any cells anymore.
This completely confuses me.
and thank you for reading/considering this thread.
Let's see what your two properties do.
delaysContentTouches: If the value of this property is true, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is false , the scroll view immediately calls touchesShouldBegin(_:with:in:). The default value is true.
canCancelContentTouches: If the value of this property is true and a view in the content has begun tracking a finger touching it, and if the user drags the finger enough to initiate a scroll, the view receives a touchesCancelled(_:with:) message and the scroll view handles the touch as a scroll. If the value of this property is false, the scroll view does not scroll regardless of finger movement once the content view starts tracking.
First, you set delaysContentTouches to false. So the scrollview immediately calls the content view's touch handling methods, allowing it to handle the touch. Obviously, the scroll view won't start scrolling right away because of this, even if you drag.
Second, you also set canCancelContentTouches to false. But if the scroll view isn't allowed to "take over" touches that the content already handles (by cancelling them), it is never able to start scrolling later on either. So if your touch hits a content view, there is no possible way for the scroll view to start scrolling: it isn't allowed to scroll right away because it isn't allowed to delay the content touches, and it can't start scrolling later because it can't cancel the content touches.
I don't know what happens within your cells, not sure what code you put in there. However, you should probably allow your tableview to both delay touches (that means that your cell won't handle swipes that are cancelled immediately anyway because they were intended to be scroll gestures), and to cancel content touches (that means that when you touch down and don't release, you can still start a scroll gesture after a cell became highlighted).
i had the same problem when touching a cell, the problem was that I'm using more than one UIGesture without adding ".cancelsTouchesInView = false" for each one
so if you're using a UIGesture just add Your_Gesture.cancelsTouchesInView = false
and you should be able to access your cells

Ensure UITextField stays onscreen despite scrolling

How can I ensure that a UITextField stays on screen despite not currently viewing the bottom of a UITableView? Like when I scroll up, how can it stay on-screen rather than disappear at the bottom of the screen. I want it anchored to the bottom of the screen at all times.
Edit: It does not appear when I add it as a subview.
2ndEdit: Stop docking post please..
You can add it to view that contains tableView i.e tableView superView.if your viewController has tableView.You can do like
self.view.addSubView(yourTextFied)
self.view.bringSubviewToFront(yourTextFied)
and set the frame of yourTextField approprately

ipad - Move UITextview to top when keyboard is active

I have UITextView in my iPad app and I need to move it to top, when keyboard appears
. Can you help me how to do it? Thanks
An extract from here :Apple documentation
Moving Content That Is Located Under the Keyboard When asked to
display the keyboard, the system slides it in from the bottom of the
screen and positions it over your application’s content. Because it is
placed on top of your content, it is possible for the keyboard to be
placed on top of the text object that the user wanted to edit. When
this happens, you must adjust your content so that the target object
remains visible.
Adjusting your content typically involves temporarily resizing one or
more views and positioning them so that the text object remains
visible. The simplest way to manage text objects with the keyboard is
to embed them inside a UIScrollView object (or one of its subclasses
like UITableView). When the keyboard is displayed, all you have to do
is reset the content area of the scroll view and scroll the desired
text object into position. Thus, in response to a
UIKeyboardDidShowNotification, your handler method would do the
following:
Get the size of the keyboard. Adjust the bottom content inset of your
scroll view by the keyboard height. Scroll the target text field into
view.

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;

horizontal scrolling of a scrollView in a scrollView - small problem

i have a scrollView that fits the whole screen. In that View i show some UIImages that scroll horizontally. Like in the PageControl Project from Apple.
Then, when the user taps the screen, i fade in a scrollView at the bottom with some other images. Also horizontally scrolling. Like in the ScrollView Project from Apple.
My problem is, that when i come to the end of the scrollView which was faded in, the upper scrollView also starts to drag. How can i stop that during activation of the second scrollView?
Redraw the frame limits on your first scrollView when the second enters the screen. That way, your touches won't respond to both views. This means you need a container-view to keep both views seperated from each other.
Then you just rescale it back whenever your second scrollView disappears.
Edit: or disable scroll in your first scrollview while the second is open.

Resources