Disabling zoom-in zoom-out functionality of the UIWebView - ios

I am new to iOS development and I am developing an application which will show and web page in UIWebView. I want to get rid of the default zoom-in and zoom-out functionality of the webview.

This is in the Apple Documentation:
#property(nonatomic) BOOL scalesPageToFit
Discussion
If YES, the webpage is scaled to fit and the user can zoom in and zoom out. If NO, user zooming is disabled. The default value is NO.
Interface Builder Approach:
Make sure that the "Scale pages to fit" tick box in the right-side column of the interface builder when you click on your webview is un-ticked. It's right at the top.
You're also going to need to un-tick the box that says "Multiple Touch" around half way down. That will disable pinching to zoom.
Code Approach:
You can also do it programmatically with this code:
webView.scalesPageToFit = NO;
You're also going to want to add:
webView.multipleTouchEnabled = NO;
That will disable the ability to pinch and zoom, since it disables the ability to use multiple fingers in an action, and pinching requires two, obviously!

set the UIWebView property scalesPageToFit to NO, will disable the user zooming
myWebView.scalesPageToFit = NO;

One simply needs to do:
myWebView.scalesPageToFit = NO;
and also disable the pesky pinch to zoom:
webView.multipleTouchEnabled = NO;

For me the ideal solution was, surprinsingly, in the html side only. Just add this in <head>:
<meta name="viewport" content="user-scalable=0" />
I don't recommend to use webView.scalesPageToFit = false because it brings issues like making everything 4x bigger. I also did not use initial-scale=1.0, maximum-scale=1.0 nor minmum-scale=1.0

UIScrollView *scrollView = [webView.subviews objectAtIndex:0];
scrollView.delegate = self;//self must be UIScrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return nil;
}
try this ... It works..

Disable bouncesZoom of the scrollView will solve your problem. Give it a try!
webView.scrollView.bouncesZoom = false

When inheriting from UIWebView you can implement. (Works from iOS 5, lower not sure)
- (UIView *) viewForZoomingInScrollView:(UIScrollView *) scrollView
{
return nil;
}

Try this:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return nil;
}

Swift Solution
Simply set the value mentioned above to false: webView.scalesPageToFit = false

To add to the answers, this will work:
webView.scrollView.multipleTouchEnabled = NO;

i am also struggle at first when i use webview scroll event after that i fond the below code for disable the scroll in webview and this also remove the multitouch gestures function in that particular webview.
[[[theWebView subviews] lastObject] setScrollEnabled:NO];
here theWebView is the name of my UIWebView..
use this code in webViewDidFinishLoad function ,,, its really helpful to me,..

Related

Key-Frame Animation appears fuzzy when moving Frames?

I use JazzHands to create a key frame based animation in a UIScrollView.
Here is an example. Look at the view at the top. When you move from page to page. While the animation is running the view at the top is slightly moving from left to right. The animation appears a bit fuzzy.
Here is the code taken from the example here:
IFTTTFrameAnimation *titleView1FrameAnimation = [IFTTTFrameAnimation new];
titleView1FrameAnimation.view = self.titleView1;
[self.animator addAnimation:titleView1FrameAnimation];
[titleView1FrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(1)
andFrame:self.titleView1.frame]];
[titleView1FrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(2)
andFrame:CGRectOffset(self.titleView1.frame, timeForPage(2), 0)]];
[titleView1FrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(3)
andFrame:CGRectOffset(self.titleView1.frame, timeForPage(3), 0)]];
[titleView1FrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(4)
andFrame:CGRectOffset(self.titleView1.frame, timeForPage(4), 0)]];
When running the demo take a look at the part marked with red in the following screenshot:
Edit: Here is the code containing this problem: https://github.com/steffimueller/Intro-Guide-View-for-Talk.ai
How can I make the animation running smooth and less fuzzy?
This is due to the frame rate in the JazzHands IFTTTAnimatedScrollViewController being set for non-retina displays. You need to double the number in timeForPage, and also use double the number of the contentOffset in animate, but use the original non-doubled values of timeForPage in places where you were using that for laying out the positions of views instead of using it for the animation time.
Here's a Gist of the changes you'd have to make to your example to get it working. Fixed Demo Gist
You need this method for setting the animation times:
#define timeForPage(page) (NSInteger)(self.view.frame.size.width * (page - 1) * 2.0)
And this one for setting the centers of your views based on the page dimensions:
#define centerForPage(page) (NSInteger)(self.view.frame.size.width * (page - 1))
Then you need to override the scrollViewDidScroll: method in IFTTTAnimatedScrollViewController to use double the numbers it's currently using.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.animator animate:(NSInteger)(scrollView.contentOffset.x * 2)];
}
We'll work on getting the JazzHands demo updated!
Before you start your animation call:
view.layer.shouldRasterize = YES; (seems that you are using objective-c so I put YES here, for swift should be true)
As soon as the animation is finished call
view.layer.shouldRasterize = NO; (seems that you are using objective-c so I put NO here, for swift should be false)
You should always use it when you animating a view
You can see more details about it in the WWDC 2012 Polishing Your Interface Rotations video (paid developer subscription needed)
I hope that helps you!
[EDIT]
Every Time you call the method animate set shouldRasterize to YES before it, like in the example bellow:
titleView1FrameAnimation.view.layer.shouldRasterize = YES;
[self. titleView1FrameAnimation animate:scrollView.contentOffset.x];
I've messed around a bit with the code and it seems that keeping those top views in place while the scrollview is scrolling made it jiggle left and right.
What I did is take out the title view from the scroll view and add it to the view controller view.
You can see it in action here
Later edit:
To actually see what I've changed you can check the file differences in Git. Basically I moved your titleViews (titleView1, titleView2, etc) from the scrollView to the view controller's view (so basically I've replaced all the lines that were like this:
[self.scrollView addSubView:self.titleView1]
to something like this:
[self.view addSubView:self.titleView1]
After that I've also took out the keyframe animations that were keeping your title views in place since they were not moving with the scrollview anymore. Practically I've deleted all the lines that were adding a frame animation to your titleviews from each configurePageXAnimation.
2nd question answer:
Say your screenshot view is called screenshotView. You can go ahead and create it like this:
UIImageView *screenshotView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ScreenshotImage"]];
[self.view addSubview:screenshotView];
self.screenshotView = screenshotView;
[self.screenshotView setCenter:CGPointMake(self.view.center.x + self.view.bounds.size.width, <#yourDesiredY#>)];
And animate it like this:
//screenshotView frame animation
IFTTTFrameAnimation *screenshotViewFrameAnimation = [IFTTTFrameAnimation new];
screenshotViewFrameAnimation.view = self.screenshotView;
[self.animator screenshotViewFrameAnimation];
[screenshotViewFrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(1) andFrame:self.screenshotView.frame]];
[screenshotViewFrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(2) andFrame:CGRectOffset(self.screenshotView.frame, -self.scrollView.bounds.size.width, 0.0)]];
[screenshotViewFrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(3) andFrame:CGRectOffset(self.screenshotView.frame, -self.scrollView.bounds.size.width, 0.0)]];
[screenshotViewFrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(4) andFrame:CGRectOffset(self.screenshotView.frame, -self.scrollView.bounds.size.width * 2, 0.0)]];

Disable horizontal scrolling in UITextView

Hello: I'm building an app that supports iOS 6 and higher. I have a few UITextViews throughout the app, and I noticed that on iOS 6, the text views are able to be scrolled horizontally. On iOS 7, they can only be scrolled vertically. Is there a way to restrict scrolling so that it will only scroll vertically?
I've checked out some other similar questions, but I don't want to add a UILabel to a UIScrollView.
Any help is much appreciated!
EDIT
When using the following two lines (per the answers suggested), this still doesn't work when setting content insets. Anyone know how to fix this?
Attempt to disable scroll:
tView.contentSize = CGSizeMake(tView.frame.size.width, tView.contentSize.height);
tView.showsHorizontalScrollIndicator = FALSE;
Insets:
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1) {
tView.contentInset = UIEdgeInsetsMake(kTextViewInsets, kTextViewInsets, kTextViewInsets, kTextViewInsets);
} else {
tView.textContainerInset = UIEdgeInsetsMake(kTextViewInsets, kTextViewInsets, kTextViewInsets, kTextViewInsets);
}
I would subclass UITextView and override setContentOffset:
- (void)setContentOffset:(CGPoint)contentOffset
{
super.contentOffset = CGPointMake(0.0, // Ignore the passed offset. Could also use self.contentOffset.x
contentOffset.y);
}
Try this -
mytextView.contentSize = CGSizeMake(mytextView.frame.size.width,HEIGHT_YOU_WANT);
mytextView.showsHorizontalScrollIndicator = NO;
ALso take a look at SO Question may it help you.

UIWebView pinch zoom to increase font size?

How to make UIWebView increase font size when pinch zoom?
If I set scalesPageToFit = YES, pinch zoom will zoom the web page, but the width will be larger than device width. I want the width to be still the device width, but the font should be larger.
There is no pure objective C way that can let you achieve this. However you can use some JS trick to increase the font size only. Try using:
//determine current "fontSize" with the help of some pinch gesture recognizer
NSString* contentHeight = [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"scaleFont(%d)",fontSize]];
webView.scrollView.contentSize = CGSizeMake(webView.scrollView.contentSize.width, [contentHeight floatValue]);
And in your html page, create a JS function similar to the following one:
function scaleFont(fontSize) {
document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust=fontSize;
return document.documentElement.offsetHeight;
}
You would still need to determine the intended fontSize in your objective-C code through some pinch gesture recognizer or something similar. Please also make sure to disable default zooming functionality of the UIWebView using scalesPageToFit = NO; and removing/disabling any "viewport" meta tag in your html doc.
Hope it helps.

Simple zoom in for UIScrollView inside of UIView

Hi I have a UIScrollView inside of a UIView. I have tried to use code snippets that I found online but they simply don't change anything. Also they are mostly for an image or custom view done within UIView, whereas in my case I have an array of programatically created UILabels. I have tried to change boundary values as well, it simply does not do anything. This is basically how I establish the size of it within viewDidAppear:
[scrollView setContentSize:CGSizeMake([screenView getWidth], [screenView getHeight])];
scrollView.showsHorizontalScrollIndicator = true;
scrollView.showsVerticalScrollIndicator = true;
screenView is a UIView variable.
This is the settings that I use(also in viewDidAppear):
doubleTapRecogniser = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(doubleTapResponse:)];
[doubleTapRecogniser addTarget:self action:#selector(doubleTapResponse:)];
doubleTapRecogniser.delegate = self;
doubleTapRecogniser.numberOfTapsRequired = 2;
[self.scrollView addGestureRecognizer:doubleTapRecogniser];
This is how I implemented my double tap method:
- (void) doubleTapResponse:(UITapGestureRecognizer *)recogniser
{
CGFloat newZoomScale = self.scrollView.zoomScale / 1.5f;
newZoomScale = MAX(newZoomScale, self.scrollView.minimumZoomScale);
[self.scrollView setZoomScale:newZoomScale animated:YES];
}
When I use NSLog messages within my doubleTapResponse, I can get responses from my console. However it does not do anything. What could be the problem?I am using iOS6.1
The error clearly says that the run time searched for a method named doubleTapResponse in the scrollview class you are using. Even if changing the target to self doesn't work, its the method definition place you have to change either the scrollview or the viewcontroller.
[doubleTapRecogniser addTarget:scrollView action:#selector(doubleTapResponse:)];
should be
[doubleTapRecogniser addTarget:self action:#selector(doubleTapResponse:)];
because the scrollview does not know what that method doubleTapResponse is.
Currently it is throwing an exception because it is trying to call the target of the UISCrollView with your doubleTapResponse method, you must add the target of self, and implement this method yourself. In here goes the logic for zooming I presume.
You must also define: doubleTapResponse in your viewcontroller (or class that you are using)
see this for more info:
Ray Wenderlich guide
In order to zoom please look at the following article: QUESTION

Force UIWebView to redraw?

Are there any techniques to cause a UIWebView to redraw itself? I've tried setNeedsDisplay and setNeedsLayout on the UIWebView and its UIScrollView, but neither have worked.
Literally found the answer right after asking. The key was to tell the subviews of UIWebView's scrollView to redraw themselves - particularly the UIWebBrowserView.
- (void) forceRedrawInWebView:(UIWebView*)webView {
NSArray *views = webView.scrollView.subviews;
for(int i = 0; i<views.count; i++){
UIView *view = views[i];
//if([NSStringFromClass([view class]) isEqualToString:#"UIWebBrowserView"]){
[view setNeedsDisplayInRect:webView.bounds]; // Webkit Repaint, usually fast
[view setNeedsLayout]; // Webkit Relayout (slower than repaint)
// Causes redraw & relayout of *entire* UIWebView, onscreen and off, usually intensive
[view setNeedsDisplay];
[view setNeedsLayout];
// break; // glass in case of if statement (thanks Jake)
//}
}
}
I've commented out the if statement to be safe and avoid reliance on UIWebBrowserView's class name not changing. Without it, it hits all UIViews that are in the scrollview, which isn't really a problem at this point (no significant overhead incurred) but could always change.
EDIT:
In some cases, the following snippet of JavaScript will accomplish the same/similar thing:
window.scrollBy(1, 1); window.scrollBy(-1, -1);
You'd think UIScrollView's contentOffset would do this too, but that's not always the case in my experience - for some reason window.scrollTo is special in this regard.
Gist: https://gist.github.com/matt-curtis/5843862
This save my life:
self.wkWebView.evaluateJavaScript("window.scrollBy(1, 1);window.scrollBy(-1, -1);", completionHandler: nil)
Add this line on webview did load delegate event or wkwebview did finish navigation
Thanks MAN!!!!

Resources