UIWebview re-enables scrolling even after scrollEnabled is set to NO - iphone

Here is a problem. I am using a draggable image in UIWebview. The Source code: here:http://ios-blog.co.uk/tutorials/rich-text-editing-a-simple-start-part-7/
It simply disables the scrolling during drag and reenable it when the dragging is completed.
It works perfectly fine when UIWebview is first brought up and hereafter. As soon as the edited text length is longer than the screen. UIWebview ignores the scroll setting of its scrollview and re-enables the scrolling.
The way I disable the scroll view is using this:
webview.scrollview.scrollEnabled = NO;
Please tell me where I was wrong. Much appreciated.

This problem is caused because of the following wrap placed within the html file I loaded as a template editing section.
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;"/>
<style type="text/css">
html, body {height: 100%;}
body {
margin: 0;
}
#wrap {
height: 100%;
width: 100%;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
}
#content
padding: 10px;
}
</style>
Any div wrapped with this block will always allow itself to be scrolled.
Deleted it and things turned out to function properly again!

may use
for(UIView *aView in webview.subviews){
if([aView isKindOfClass:[UIScrollView class]])
aView.scrollEnabled = NO;
}
this makes sure that any scrollview's scrolling is disabled.

Related

Background image not resizing when switching from portrait to landscape

I have the following CSS:
#media screen and (orientation:portrait) {
body {
background-image: url(../img/background.jpg);
background-size: auto 100%;
}
}
#media screen and (orientation: landscape) {
body {
background-image: url(../img/background.jpg);
background-size: 100% auto;
}
}
That works fine on every device except iPhone with iOS 6.3 and smaller (works fine with iOS 7)
On iOS 6 while switching from portrait to landscape, the landscape screen is only covered half way by the background img, any idea why that is?
After doing some research I found the bug.
In the "meta" tag if you specify the "height" property, the view will NOT cover the whole width in Landscape mode.
Bad Code:
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1.0, minimum-scale=1, width=device-width, height=auto, target-densitydpi=device-dpi" />
Good Code:
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, target-densitydpi=device-dpi" />

How to add views to UIWebVIew? like this image

I wonder how some apps do this great affect, i've trying to accomplish this but without much success.
the idea is to add views like textview or label to a webview or to scollview so that it appears with gray background and can be scrolled, the below image describes the intended goal :
I would be very much appreciated if you help me.
Use following code and just background color your UIView.Hope It will helps you.
[webView setBackgroundColor:[UIColor clearColor]];
[webView setOpaque:NO];
Use a pure HTML/javascript/CSS solution, not a native UIVIew laid on top of the UIWebView
CSS like this creates an initially invisible box, 400px wide, 120px from top, white background, bordered:
.floater
{
display: none;
position: absolute;
top: 120px;
left: 50%;
width: 400px;
margin-left: -200px;
z-index: 200;
background-color: #ffffff;
border-top: solid 2px #b0b0b0;
border-left: solid 2px #b0b0b0;
border-bottom: solid 2px black;
border-right: solid 2px black;
}
Use this class="floater" on your label, div, whatever.
You can then use the display style to hide/unhide it via javascript.
If you want your content to scroll within the webView, You have to add the view as a subview of the first subview of the webview.
The scrollview is the first subview of the webview
[[[webview subviews] objectAtIndex:0] addSubview:imageview];

Workaround for 'background-attachment: fixed' which is not working in iOS4

I'm struggling to get web page with a fixed background image, so the image does not move when page is scrolled in a UIWebView.
What I've discovered is: background-attachment: fixed does not work in iOS4 (using 4.2.1).
To double-check I've prepared a page with code snippet (below) inside <head> section and the page works as expected under Safari and Firefox on Mac, but fails to do so in iPhone's Safari...
What do you suggest as a workaround for achieving the expected results? I've made my UIWebView translucent and added UIImageView, so I can see "fixed background image" through translucent page. Unfortunately, I can see UIWebView borders when I scroll over its end/beginning edges.
Is there any official Apple resource/web page stating that background-attachment: fixed is not implemented for iOS4?
Cheers!
P.S. The code snippet referred above:
<style type="text/css">
body {
background: #ffffff url('image.jpg') fixed no-repeat;
background-attachment: fixed;
}
</style>
I am not sure what is going on with the CSS and have not had a chance to check it out for myself but I know when I was trying to get rid of the shadows from a UIWebView I used this bit of code:
NSArray *sv = [NSArray arrayWithArray:[myWebView subviews]];
UIScrollView *webScroller = (UIScrollView *)[sv objectAtIndex:0];
NSArray *wsv = [NSArray arrayWithArray:[webScroller subviews]];
[[wsv objectAtIndex:6] setHidden:YES];
[[wsv objectAtIndex:7] setHidden:YES];
[[wsv objectAtIndex:8] setHidden:YES];
[[wsv objectAtIndex:9] setHidden:YES];
and it got rid of the shadows. I thought I got the answer off of a SO question but when I looked for it this is the only one that came up.
It passed App Store inspection.
Use a div for the background with a negative z-index:
<head>
<style>
#background {
background: url("background.jpg") no-repeat;
position: fixed;
top: 0;
left: 0;
background-size: 320px 480px;
width: 320px;
height: 480px;
z-index: -1;
}
</style>
</head>
<body>
<div id="background"></div>
This body text appears over the fixed background and scrolls.
</body>
Works on iOS 5 and iOS 6.

UIWebView not autoresizing to fit screen

I have an app that uses a UIViewController to display articles in a UIWebView, some of which have images. I have it set up so that when an image is clicked it calls
- (void)displayImage:(NSInteger)i {
ImageViewController * imageVC = [[ImageViewController alloc] initWithImageId:i];
[self.navigationController pushViewController:imageVC animated:YES];
[imageVC release];
}
This loads another UIViewController.
All of my views responds perfectly fine to rotations. The ImageViewController maintains the proportions of the image and my ArticleViewController fills the screen with text. However, whenever I rotate the screen to landscape mode while viewing my ImageViewController and press the back button. The previous UIViewController autoresizes incorrectly, increasing the font size of the text rather than putting more words on a line.
EDIT:
This is an article view controller:
This is the ImageViewController that comes up when you click the image:
Now if we change the orientation here...
And hit the back button, the article content has been inappropriately resized to fit the window.
This can be corrected by turning the iPhone twice. This is what the article should look like in landscape.
The problem has something to do with the UIWebView not autoresizing appropriately when it isn't currently visible. What can I do to fix this?
You need to do this in Interface Builder.
Click on your UIWebView and press Apple-3 to pull up the "Size Inspector".
Under "Autosizing", make sure the two arrows inside the box are selected. This will make sure the view size is always maximized to its container.
You can change it on server side by adding different css styles depending on device orientation:
for example:
/* Landscape */
#media screen and (min-width: 321px)
{
body{
width: 320px;
}
}
/* Landscape */
#media screen and (min-width: 321px)
{
body{
width: 480px;
}
}
or you could change viewport from client side:
In case your page has got viewport meta tag:
<meta name="viewport" id="view" content="width=320px, minimum-scale=1.0, maximum-scale=1.0">
then in your code you can change this viewport every time orientation changes:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if(interfaceOrientation == UIDeviceOrientationLandscapeLeft)
[uiwebview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"document.getElementById(\"view\").setAttribute('content', 'width=480');"]];
}
....
and change it again when orientation is portrait
For future reference. You can do this programmatically with this.
[webView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];

Dragging in UIWebView

Is there a way to prevent a UIWebView from being dragged?
Also, is there a way to actually overwride the common UIWebView functions such as highlighting?
Hope someone can answer me.
Thanks
webview.scrollView.scrollEnabled = NO;
You can look up the scrollView of the webView and disable the scrolling with this code:
UIScrollView *scrollView = nil;
for(UIView *aView in webView.subviews){
if([aView isKindOfClass:[UIScrollView class] ]){
scrollView = (UIScrollView *)aView;
scrollView.scrollEnabled = NO;
scrollView.bounces = NO;
}
}
To prevent the drag:
<script type="text/javascript" language="javascript">
document.ontouchmove = function(e)
{
e.preventDefault();
}
</script>
To override the highlighting:
<style type="text/css">
body
{
-webkit-user-select: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-touch-callout: none;
}
</style>
So, on the highlighting part:
The 1st line disables selection (like on text)
The 2nd line disables the actual highlight
The 3rd option disables the "popup" you see when you press an image for 2 seconds, asking if you'd like to download/save
Keep the ones you need.
Hope this helps :)