How to get the contentOffset of a UIScrollView mid-scroll? - iphone

How would one get the contentOffset or similar information about a UIScrollView while the user is still scrolling? For example if I wanted to place an image as a header right above the content of a UIWebView, without the image being part of the WebView's scrollView, but have it update while the user scrolls, not just jumping to the position after they let go. How could I go about doing this?

In iOS 5, the UIWebView has a scrollView property which is the UIScrollView responsible for handling the scrolling. Prior to iOS 5 you can search the UIWebView's subviews property for the subview using [subview isKindOfClass:[UIScrollView class]].
Once you have the UIScrollView, you can then set its delegate property. In the delegate you can then respond to the scrollViewDidScroll: message when the user scrolls, and update the position/contents of your header view.
Added: 29/2/2012 - In order to preserve the UIWebView's functionality, you will need to forward your intercepted UIScrollViewDelegate methods to the UIWebView too. To do this, you can call the UIWebView at the end of all mandatory methods within the protocol and implement something like forwardInvocation: or forwardingTargetForSelector for the optional protocol methods. This will preserve your original UIWebView behavior and allow you to enhance it with your own logic.

Why can't you just put the header as a separate view above the UIWebView? Why does it have to be a subview?

Related

UIScrollView Not Scrolling In iOS 7 StoryBoard

I am trying to add a UILabel along with a UIWebView together in a view controller and want them to scroll together. So to accomplish that, i have added a UIScrollView behind these two views but the UIScrollView is just not scrolling. I am using Storyboard and iOS 7 SDK.
I have seen many questions but they are not of much help. Some are suggesting that i should disable the AutoLayout that i cannot do due to the requirements of my project. What else can i do to make it work? Thanks!
According to Apple's documentation, you shouldn't use UIWebView inside UIScrollView. What you can do is to add your UILabels and UIWebView inside aother UIView and then add this UIview inside the UIScrollView.
Secondly, you have to set the contentSize of the UIScrollView in order for it to work.
You also have to disable the scrolling of UIWebView that is embeded inside the UIScrollView.
Still if your problem is not solved. It is better to just turn of AutoLayout. I have had the same issue and i wasnt able to solve it without disable the AutoLayout.
The documentation for UIWebView states:
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.
You need to wrap the label and web view in a regular UIView. Add the regular view to the scroll view.

UIScrollView type access to UIWebView

I'd like to know a UIWebView's contentOffset, and/or get didScroll type notifications from it. Am I missing something, or is that impossible.
The thing I'm going for is a UI that's half info panel, half UIWebView. When the app finds out that the user wants to see more of the web view, I'd like to reframe with a smaller info panel and larger web view. When user wants to see less, reframe to half and half.
The problem is the interface is pretty much out of button space. If the web view were a scroll view, I could use scroll actions to discern intent. (scroll down means enlarge, scroll above the top means reduce). Any ideas how I can achieve this with a web view?
UIWebView is made up of UIScrollView.
to access the ScrollView you could use either of the two ways :
UIWebView *aWebView; // Initialization code
UIScrollView *aScrollView;
for (UIView *aView in [aWebView subviews]) {
if ([aView isKindOfClass:[UIScrollView class]]) {
aScrollView = (UIScrollView *)aView;
}
}
OR
aScrollView = [[aWebView subviews] objectAtIndex:0];
Option-1 is a safer way to get the UIScrollView instance...
AND....
If you are building your app for iOS5, you could just call..
aScrollView = aWebView.scrollView;
EDIT
If you check UIWebView in detail, you should have noticed that it conforms to UIScrollViewDelegate
If your primary intent is to provide custom gesture-type action on top of the UIWebView, you can try UIGestureRecognizer.
(Do not forget to provide UIGestureRecognizerDelegate's gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer implementation to allow webview own gesture recognizers to work properly)

accessing delaycontenttouches property of uiscrollview

Since uitableview inherits from uiscrollview, would i have access to the delayContentTouches property of UIScrollView from my table view controller? I thought I could do something like the following, but it doesn't work(self is the UITableViewController):
self.view.delayContentTouches = YES;
Thanks.
If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. This value is YES by default. You'd better tell what do you want to do?

How to scroll the outside scrollView not inside scrollView?

I have a scrollView and inside it some scrollViews , and everyone have a webview to show html file, using this method I will zooming every html file , but Now I found that when I scroll the html file it didn't work. I think I scroll the scrollview inside and it autosize the html file , but How to let the scrollview inside not to call the scroll function and when I scroll the html it can call the scroll function outside, and I have disable the scroll property of inside scrollview, but it didn't work, and How to do with it? Thank you very much!
UIScrollView in UIScrollView is a common use. See apple stocks app...
I am trying to send ScrollView Gesture events like pan and zoom from outside the scrollview itself. It doesn't look like apple lets you send to the UIScrollViews handlePan: and handleZoom: methods
A quote from UIScrollView Class reference :-
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
I would add : "You should not embed UIScrollView objects in UIScrollView "

determine if uiview is displayed

is there a possibility to determine if an uiview obj is going to be displayed. imagine: you have 2 uiviews in an uiscrollview. now you are going to switch per gesture from the first view to the second. the first view now is NOT in the viewport. now you are going to go back to the first view. and now I want to be notified that this view is in viewport, or is redisplayed. the same has to be for the second view. I have not found any callback or something like this.
You make sure your UiViewController overrides viewWillAppear: (before it appears this method is called) or viewDidAppear: (after this method is called).
See: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewWillAppear:
That depends what you mean by "switch". If one view is just scrolled out of the visible area of the scrollview, but still remains attached as a subview to it, then you may want to check if the bounds of your view overlap those of the scrollviews visible area.
You could do this by using UIScrollView Delegate's scrollViewDidScroll: method to implement a check for overlaps while the user is scrolling.
If however your view is actually removed from the viewstack, then you may want to subclass UIView and implement willMoveToSuperview: to check if the view has been added to the scrollview again.