UIWebView scrollTo hindered by a return to top - uiwebview

I have a ViewController with a UIWebView, in a Objective-c project for iPad.
When I add html (with loadHtmlString), I try to keep the scroll position it had before the loadHtml, but I cannot help and it returns to the top.
Here are the lines I use:
scrollPosition = [[webView stringByEvaluatingJavaScriptFromString: #"scrollY"] intValue];
[webView loadHTMLString:html baseURL:nil];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0, %d);",scrollPosition]];
I tested the value 'scrollY' at different locations in my code
and it stays at the value scrollPosition that I want, but when it ends up showing on the screen the WebView is always at the top (with the scrollY value then to 0).
Is there someone who could help me figure out why this happens?
I checked for hours the answers online, also on StackOverflow.
I tried different solutions (like adding in my webview the JavaScript "onclick="scroll(); return false;", but to no avail.
I tried to self-delegate the webView and update the scrollTo in webViewDidFinishLoad, but it seems that webViewDidFinishLoad is not called (the breakpoint is not reached) although I did add in .h in the interface and in the .m in ViewDidLoad the line: webView.delegate=self;
Thank you in advance for any help.

I realized that after recompiling, webViewDidFinishLoad was called after the html was updated, and that I could then scroll to the correct position.
For the self delegation I used in viewDidLoad, it looks so:
webview.delegate = self;
Before updating the html in the webView I calculate the scrolling position with the code:
scrollPosition = [[wbView stringByEvaluatingJavaScriptFromString: #"scrollY"] intValue];
Then, for moving back to this position once the webView has an updated html, I then use:
-(void) webViewDidFinishLoad:(UIWebView *)wbView {
[wbView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0, %d);",scrollPosition]];
}
I found part of the solution through the post below, with the answer of Pawel:
How can I scroll programmatically to the bottom in a uiwebview

Related

UIWebview scrollview changes contentsize for no reason

I have a uiviewcontroller which loads a uiwebview. When the uiviewcontroller loads the uiwebview everything is done correctly. But when one navigates away and returns back and the uiwebview is not loaded, it is displayed differently. The uiwebview scrollview's contentsize height changes by 64px, it gets higher.
When it is loaded for the first it looks like this
When one returns back to the view, this is what happens
I hope I was able to make my problem understandable. If more details are necessary, I am able to give more. Thank you
I had a similar problem, try this code in your viewDidLoad method.
if([self respondsToSelector:#selector(setAutomaticallyAdjustsScrollViewInsets:)])
self.automaticallyAdjustsScrollViewInsets = NO;
You can do like this.
- (void) viewDidLoad
{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
}

CScrollView Offset Client Rect with Scroll Position

I am trying to write a function that will work out if the window that currently has focus is entirely shown in the client rect of my CScrollView but I am struggling to work out what I am doing wrong. This is what I have thus far:
CWnd * pWnd = pView->GetFocus();
if(pWnd)
{
CRect winRect;
pWnd->GetWindowRect(&winRect);
pView->ScreenToClient(&winRect); //pView is a pointer the CScrollView
CRect viewRect;
pView->GetClientRect(&viewRect);
CPoint currentScrollPoint = pView->GetScrollPosition();
viewRect.OffsetRect(currentScrollPoint);
if(!(viewRect.PtInRect(winRect.BottomRight()) && viewRect.PtInRect(winRect.TopLeft())))
{
//Not shown fully
}
}
Can anyone see what I am doing wrong here or suggest a better way of doing this?
The comments to the question above cleared up the actual intent of the question:
...when I tab to one that is not shown by the current client rect I want to scroll
to display that `CEdit`...
I found two articles searching MSDN for CFormView scroll tab key:
the first one uses OnCtlColor() to check if a sub-window has the focus and is not in view; it uses ScrollToPosition()
the second one mentions that ScrollToPosition() does not work in Windows CE (both the articles are quite old!), checks for WM_KEYUP of the tab key in PreTranslateMessage() and uses it's own ScrollToPos() function to scroll the control into view (this article was meant for Windows CE and you will need to replace wce_GetNextWindow by GetNextWindow

Changing StatusBar orientation has no effect

I have two ViewControllers - the main one is Portrait only - the second, which displays a WebView loaded with a YouTube video can rotate to any orientation. When it is dismissed in Landscape, returning to the main ViewController, the status bar is left in Landscape mode. I know when the YouTube view is dismissed and have placed the following line on code in the called method:
UIApplication.SharedApplication.SetStatusBarOrientation(UIInterfaceOrientation.Portrait,true);
But it has no effect on the status bar. Is there some other place to set the orientation? Other ideas?
Thanks,
Rick
Fixed the problem by adding the following to the AppDelegate
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations (UIApplication application, UIWindow forWindow)
{
return UIInterfaceOrientationMask.AllButUpsideDown;
}
Then this code actually did what it is supposed to:
UIApplication.SharedApplication.SetStatusBarOrientation(UIInterfaceOrientation.Portrait,true);

How to perform transition between controllers when using monotouch and storyboard?

I have created storyboard using following instructions
http://docs.xamarin.com/ios/tutorials/Introduction_to_iOS_5#Storyboards
It works as expected.
Then I added new button to MonkeyController and new ViewController to storyboard. This is the code I tried to use in button's click event
partial void btnClicked (NSObject sender)
{
UINavigationController ctrl = this.ParentViewController;
var screen = new TableRowViewController();
ctrl.PushViewController(screen, true);
}
It opens TableRowViewController, but it's background is black and it does not show any modifications I made on it on storyboard.
Can you help me with this?
I'm no expert yet, but it has been my experience trying to get it to work properly that if one of the sides of the split-view is black, then there is a segue that is illegal.
Hope that helps...

UIScroller inside UIWebView

Does any one have an idea how to access the UIScroller class , which is the default subview of UIWebView ?
I want to handle the touches, zooming , panning and scrolling features inside the webview .
Thanks..
I know this thread is old but if anyone comes across it there's a new way.
As of iOS 5 UIWebView now has a property called scrollView which you can't replace but you can set the properties of it. Most people just want to disable zooming/bouncing/scrolling all together which can be done by setting the properties of the scrollView for example if webview is a UIWebView:
webview.scrollView.bounces = NO; //Disables webview from bouncing
webview.scrollView.minimumZoomScale = webview.scrollView.maximumZoomScale = 1.0; //Forces zoom to be at 1 (can be whatever you fancy) and disables zooming
webview.scrollView.bouncesZoom = NO; //Disables bouncing when zooming exceeds minimum or maximum zoom
I suppose you could set the delegate for the scrollView if you want more control, though to be on the safe side you might want to store the original delegate and call its methods appropriately in your custom delegate.
Handling the touches would be more difficult since you can't replace the scrollView to provide your own handlers. Best you could do is add some gesture recognizers as part of the UIView and try to handle them there, but I think UIWebView will still receive the events. Alternatively in iOS 5 they let you access the gesture recognizers directly on UIScrollView.
You can find this by going like this:
[webview objectAtIndex:0]
That should be where it is. If not, put this in your code somewhere and run the program to search for the index of the UIScroller, and replace the 0 above with that index:-
for (UIView *subview in [webView subviews]){
NSLog(#"subviews of webView : %#", [[subview class] description]);
}

Resources