Partial black background in UIWebView on iOS7 after setting content inset - ios

I have a problem with UIWebView on iOS7:
First I'm setting the content inset for my web view scrollview:
[webView.scrollView setContentInset:UIEdgeInsetsMake(40, 0, 0, 0)];
Then when web view loads an empty page there is a black rectangle at the bottom of the web view, that has the height 40.
It's for sure because of content inset, and I met this problem only on iOS7. How can I solve this? Any help will be appreciated!

Try this code, it is not the best solution, but it will get rid the black rectangle:
[webView.scrollView setContentInset:UIEdgeInsetsMake(40, 0, -40, 0)];

From my experience, iOS7 UIWebView does not play nice at all with custom insets and content size. It always breaks under certain conditions. We use a webview for a rich text editor, so we had to manage a volatile contentEditable="true" webview.
What we ended up doing was to take the UIWebDocumentView/UIWebBrowserView (let's call it "internal view") from the UIWebScrollView and add it as a subview in a scrollview of our own. To do this safely, we iterate the subviews of webView.scrollView and took the one whose class name has a UIWeb prefix. (You have to remember that iOS6 UIWebScrollView contains shadows as well.)
Now you have to manage your scrollview. What we did was observe changes of the frame property of the internal view and update the scrollview's contentsize accordingly.

Related

Swift - contents within scrollview get chop off

Facing an issue when scrolling, a part of content didn't displayed.
https://i.stack.imgur.com/70FIC.png
Here it is what is supposed to be displayed
https://i.stack.imgur.com/ViPjF.png
My UI is designed in storyboard, the structure for the scrollable component is as below:
UIView -> UIScrollView -> UIStackView -> Multiple UIViews
If wondering how I take the screenshot of the one that's supposed to be displayed, the price is a drawer component, somehow after I tapped the drawer component, the content will be displayed, but after I scroll to top, same issue will happen to the content, which they get chop off.
https://i.stack.imgur.com/OKSEY.png
Updates:
Trying to put background colour on views to see which view is blocking the scrollview.
https://i.stack.imgur.com/vZBY7.png
Found that the scrollview (Booking DetailsSV) blocked by its superview (Booking Details View) in the hierarchy. Here is the screenshot taken in the test phone.
https://i.stack.imgur.com/Wm7xa.png
Is it suppose to happen like this?
Your scrollview doesn't seem to have correct contentSize. You need to set content size of your scrollView like following:
let sizeOfContent = 500
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, sizeOfContent);
You can do this in viewDidLoad.

How to get rid of adjustedContentInset in UIWebview?

I have a UIWebView in which i load content from remote URL. Problem i am facing is, last part of the content of webview is not scrollable. When debugging i noticed that adjustedContentInset is set as {0, 0, 49, 0}. Perhaps that is the reason partial webview content(49 pts from end) is not scrollable. Is there any way i can get rid of adjustedContentInset so that the whole content becomes scrollable?
I tried setting the contentInsets of webview's scrollview to {0,0,0,0} but it didn't help.
UITableView and UICollectionView has contentInsetAdjustmentBehavior method to reset adjustedContentInset but i couldn't find such property for UIWebView.
Any help will be appreciated.
Actually, contentInsetAdjustmentBehavior is a UIScrollView property, that's why what you mentioned:
UITableView and UICollectionView has contentInsetAdjustmentBehavior
method to reset adjustedContentInset but i couldn't find such property
for UIWebView.
is correct.
That means contentInsetAdjustmentBehavior is not a property for the UIWebView (UIView), what would you need to do is to access it via the web view scrollView property:
The scroll view associated with the web view.
Discussion
Your app can access the scroll view if it wants to customize the
scrolling behavior of the web view.
which exactly what are you aiming to. Therefore:
webView.scrollView.contentInsetAdjustmentBehavior = ...
should work.

How to make a vertical scrolling view on iOS?

Can someone please guide me to a tutorial , on how to implement a vertical scrolling view on my iOS app?
I can't believe that the last 2 days , I can't find a single working example on just a vertical scrolling view. All the tutorials are about horizontal scrolling, zooming etc etc.
What I am looking for would be something like this tutorial http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone-2/, which shows how to make a uiscrollView and add objects to it, from the Builder and not programmatically. The only flaw I found was , that when trying to add a map down in the scrolling area, the map would appear up and not in the position I placed it. Maybe any ideas on that too?
Anything in mind?
So you want to create a scroll view in xib.
Add a scroll view to the nib file
set its size to to whatever you want
Add your controls (Buttons,labels..)
Add it as a sub view of main view
Finally create a property of scroll view and in viewDidLoad set the content size of the scroll view
self.scrollView.contentSize =CGSizeMake(320, 700);
It simple, same as horizontal scroll view.
Add a scrollview in a view hierarchy,
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:scrollView];
Now to make scroll view scrollable, set its scrollview content size greater than its bound.
[scrollView setContentSize:CGSizeMake(scrollView.bounds.size.width, scrollView.bounds.size.height*3)];
Now it can scroll three time the height of scrollView.
I've struggled with this simple issue for a full day. There are most likely benefits to a more sophisticated approach, but unchecking autolayout solved my issue.
For the quick fix, I believe this is the best approach and will save you a HUGE headache.
Just change the content size of your scrollview.Let's assume you are creating a UIScrollView with the following frame
scl=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 620, 172)];
Now call the content size property of your scroll view,like i have shown below .
[scl setContentSize:CGSizeMake(700, 172)];
Now add the scroll view on a view / View Controller etc and check it .

UIScrollView not working

I have a UIScrollView with a View inside of it. Inside of that view is a bunch of buttons, labels, etc that fit in the View when in Portrait mode...When the iPad is rotated, I want the scrollView to kick in so the user can scroll to the bottom of the view. The app runs, but when I rotate it, the scroller never works...I believe I've wired everything up correctly and I have this code in the viewDidLoad event:
[scrollview addSubview: masterView];
scrollView.contentSize = [masterView sizeThatFits:CGSizeZero];
Is there something else I am missing? Do I need to modify the size when the iPad rotates?
thanks
There may be a problem with the content size of the UIScrollView. Without the contentSize being set larger than the actual scrollView size, scroll bars won't be shown.
You can code this in with something like this:
[scrollView setContentSize:CGSizeMake(2000,2000)];
And then changing the content size to the actual content size of what you are putting in the UIScrollView (scrollView).
If you look at the results of [masterView sizeThatFits:CGSizeZero] (e.g. NSLog or set a breakpoint in your debugger, I think you will find that it's not what you expected it to be. You might find that masterView has autoResize parameters set (which is common for a view that covers the entire screen), which means that it might, itself, be getting resized too short to fit all of its controls and scrollView is simply grabbing this shortened value itself. Take a look at that CGSize and the problem will be obvious.
I faced similar situation but my case was iPhone.
Remember that content should be larger than scroll for scrollView to kick in.
"Why would you want to go down if everything is visible in front of you ?"
use the following code:
[scrollView setContentSize:CGSizeMake(intValue, intValue)];
intValue: Integer values setting width and height of scroll.
Even if it doesn't works, don't worry there are loads of other options to figure out the solution:
1. NSLog
2. Breakpoints
3. Put up errors you are getting from console on stackoverflow

how to forward UIWebView scroll to UIScrollView ? (conditionally)

I have a UIWebView inside UIScrollView.
This idea is to be able to create more reading space on screen when user scroll the webpage upwards - by scrolling the UIScrollView upwards till the toolbar is visible, and obviously when the toolbars is nomore visible actually scroll the webpage to show more content that's on the page.
IPhone Safari browser does exactly the same functionality.
see screenshot (first) for default behavior i am getting - i guess because : the scrolling message is consumed by the webview since the touch/scroll is happening directly on webview area.
what i would like to do here is programatiicaly decide when to forward the 'upward scroll' to the UIScrollivew and when not to.
Any tips on how to get around this will be so helpful. Thanks!!
The UIWebView class reference discourages embedding a UIWebView in a UIScrollView:
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.
However I suppose you still want to implement your feature ;-)
Idea 1: Don't embed the UIWebView in a UIScrollView, instead run javascript using UIWebView's stringByEvaluatingJavaScriptFromString: method, modifying the DOM to add a toolbar below. If required, you can callback objective-c code from any buttons pushed on the toolbar by registering some custom URL schemes.
Idea 2: Don't embed the UIWebView in a UIScrollView, but in a normal UIView. Building on Vignesh's suggestion, listen for your webView's inner scrollView's scrollViewDidScroll: callback via the delegate, checking the contentOffset vs. the contentSize's height each time the callback is called. Once they are equal, it means you got to the bottom. Once this happens you can animate your toolbar's frame to "enter" the containing UIView and "push" the webView's frame away.
Idea 3: Ignore Apple's recommendation, and embed the UIWebView in a UIScrollView. Building on Vignesh's suggestion, listen for your webView's inner scrollView's scrollViewDidScroll: callback via the delegate, checking the contentOffset vs. the contentSize's height each time the callback is called. Once they are equal, it means you got to the bottom. Once this happens set the userInteractionEnabled property of the webView to NO, and set it to YES on the scrollView which contains the webView and the toolbar. Hopefully the scroll will continue smoothly enough. Of course you have to listen to the containing scroll view in the same way to determine when to switch back the userInteractionEnabled.
A variation on this idea would be to just set userInteractionEnabled to NO for the webView, but set the webView's frame's height to match its contentSize, and also enlarge the contentSize of the containing scrollView accordingly.
Both variations have the drawback that in some cases you won't be able to do things such as click on links :-( But maybe that's good enough for your case. At least in the first variation it's not so bad.
You can add a searchbox and uiwebview in a UIscrollview one below another. To get the content offset when webview is scrolled you can use the following code snippet.
UIScrollView* currentScrollView;
for (UIView* subView in testWebView.subviews) {
if ([subView isKindOfClass:[UIScrollView class]]) {
currentScrollView = (UIScrollView*)subView;
currentScrollView.delegate = (id)self;
}
}
Now you can change your base scrollview offset's y value to the offset value you get from the scrollview didscroll delegate method.
Advanced ScrollView Techniques
https://developer.apple.com/videos/wwdc/2011/

Resources