How to disable horizontal scroll in uiwebview? - objective-c

I am using uiwebview in my code and implemented swipe function on uiwebview. For swipe function i have set YES for scaleToFir property for webview, when we give, scaleToFit=YES, content get shrinked. But for my case both needed. swipe aswell as content should not shrink. But disable horizontal bar is fine. can you please help me out?
Srini

for (UIScrollView *scroll in [webview subviews]) {
// Make sure it really is a scroll view and reset the zoom scale.
if ([scroll respondsToSelector:#selector(setZoomScale:)]){
scroll.bounces=FALSE;
}
}

Try This:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"loaded");
[webView.scrollView setContentSize: CGSizeMake(webView.frame.size.width, webView.scrollView.contentSize.height)];
}

Related

ScrollView is eating Swipe Gesture Recognizer

i have a details view controller with a scrollview on it. And i have load UILabel, UIImageView on top of UIScrollView. The scrollview is set to scroll vertically only. and the view need to be able to recognize swipe left and right to navigate to next/previous page by adding
self.leftGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipeRecognizer:)];
[self.leftGestureRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:self.leftGestureRecognizer];
So when i swipe within UILabel, it is working. If i swipe start from UIScrollView, it is not working. I guess it is the UIScrollView conflict the swipe gesture.
In short, swipe gesture is only working on the subview but not UIScrollView .Does anyone have any idea on this?
UPDATE:
If i swipe start from scrollview first then end at UILabel, it won't recognize the swipe gesture. If i swipe within UILabel(start and end in the UILAbel) it is able to recognize.
Make sure that if the UIImage allows zooming, the swipe recognizer won't work, since it is trying to zoom. If this is the case, you will need to enable the zoom only in certain case. hope it helps.
Initialize the scroll view with scrolling disabled. Then you need to disable scrolling in the scroll view when the image is not zoomed in, and renabled it when it is zoomed in. This provides the expected behavior.
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
if (scrollView.zoomScale != 1.0) {
scrollView.scrollEnabled = YES;
} else {
scrollView.scrollEnabled = NO;
}
}
To enable zoom provide an image on a delegate.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return myImage;
}
Also add the gesture to instance of scrollview like this -
leftGestureRecognizer.delaysTouchesBegan = YES;
[myScrollView addGestureRecognizer:leftGestureRecognizer];
OR You can try the following -
[scrollView.panGestureRecognizer requireGestureRecognizerToFail: leftGestureRecognizer]

iOS7 UIWebView scroll indicator disappearing

I've noticed weird behaviour in my UIWebView using iOS 7.0 SDK. This behaviour wasn't there in iOS 6, and my code regarding this webview hasn't changed.
I have a storyboard, not using autolayout, autoresize mask for the webview is everything selected.
I have scale pages to fit set to YES.
When I load a local html file, it displays fine and upon scrolling, the webview will show the scroll indicator.
When I rotate the device, the webview resizes fine, it's rect is correct size, the page displays as expected, however the scroll indicator is not visible
Here's the kicker. If I don't scroll before I rotate, the scroll indicator is visible when I start scrolling AFTER the rotate.
If I DO scroll before the rotate, the scroll indicator is not visible after the rotate.
I checked the html, there is no jquery or js that will change the overflow in regards to orientation.
OK I found the solution, I was using a subclassed UIWebView and overriding the layoutSubviews to not draw the gradient shadows above and below the scrollview (as it has by default).
- (void)layoutSubviews
{
[super layoutSubviews];
// hide the shadows at the top and bottom of the webview's frame
for (UIView* shadowView in [self.scrollView subviews])
{
if ([shadowView isKindOfClass:[UIImageView class]]) {
[shadowView setHidden:YES];
}
}
}
Well guess what was a UIImageView? The Scroll Indicator! I removed this code and the problem went away

Added whitespace in UIWebview - removing UIWebView whitespace in iOS7 & iOS8

Im loading local html files, since iOS7 there is added white space on top in the UIWebView.(I cant post an image as i do not have enough points.)
image can be seen here- snap shot from iPhone simulator, uiwebview surrounded by black frame, the html content is grey, but there is white added above it
I have tried to adjust the zoom using
[webView stringByEvaluatingJavaScriptFromString:#"document. body.style.zoom = 5.0;"];
webView.scalesPageToFit = NO;
credit to: Srikar Appal
I also set tried to remove white spacing:
NSString *padding = #"document.body.style.margin='0';document.body.style.padding = '0'";
[webView stringByEvaluatingJavaScriptFromString:padding];
credit to: thenextmillionaire
still no luck. In the desktop chrome browser there is no whitespace. The html files are Google Swiffy files - containing html and JSON.
edit: updated Image
Try self.automaticallyAdjustsScrollViewInsets = NO; in ViewDidLoad.
ios 7 add 64px automatically for scroll view. (status bar and nav bar)
This problem only affects the UIWebView if it is the first subview of the parent view. One alternative way to work around this problem is to add another non-visible empty view to the parent view as the first view. In Interface Builder add a zero size subview and use the Editor->Arrange->Send to Back menu command.
If you're not using Interface Builder, but instead are subclassing the UIWebView, then it can be done by creating a UIView instance variable called scrollFixView and overriding the following methods:
- (void)didMoveToSuperview
{
[super didMoveToSuperview];
if ([self superview].subviews.firstObject == self) {
_scrollFixView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
_scrollFixView.hidden = YES;
[[self superview] insertSubview:_scrollFixView belowSubview:self];
}
}
- (void)removeFromSuperview
{
if (_scrollFixView) {
[_scrollFixView removeFromSuperview];
_scrollFixView = nil;
}
[super removeFromSuperview];
}
I had the same problem so I tried a few things:-)
This worked for me, but correct me please if there is a better way.
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
if(self.navigationController.navigationBar.translucent == YES)
{
_webView.scrollView.contentOffset = CGPointMake(_webView.frame.origin.x, _webView.frame.origin.y - 54);
}
}
So basically you need to :
1) Add the UIWebView delegate method - webViewDidFinishLoad:
2) Then I setup an if statement to check if the translucent option is active.
The last one you only need to do of course if you give the user the option within your app.
The number after the _webView.frame.origin.y is just for my app. It may differ for you.
I solved this problem by simply setting a constraint on the WebView, setting the top space between it and the View top to 0, causing the NavBar to overlap the whitespace.
One alternative to Jeff Kranenburg's method is to subclass and override the UIWebView subclasses' UIScrollViewDelegate method scrollViewDidScroll:. This is only appropriate if scrolling is turned off for your subclass.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if ([[self superclass] instancesRespondToSelector:_cmd]) {
[super scrollViewDidScroll:scrollView];
}
[self fixUpScrollViewContentOffset];
}
- (void)fixUpScrollViewContentOffset
{
if (!CGPointEqualToPoint(self.scrollView.contentOffset, CGPointZero)) {
self.scrollView.contentOffset = CGPointZero;
}
}
I already got it .
here my logic code, When the application open the website you must get the size of your webview then set it on height
here my code
ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) webpage.getLayoutParams();
p.height = webpage.getHeight();
// check if how long you need to set your height for webpage then set it :)
Log.e(" webpage.getHeight()", String.valueOf(webpage.getHeight()));
webpage.setLayoutParams(p);
Hope you will take my code and my answer to :) works on any devices even tabs too :)

UIWebview default zoom level

In my app, I use UIWebviews to display some one page PDFs, I've noticed that in iOS5 the default zoom level is to show the entire document where as in 4.3 the document was opened zoomed in.
Is it possible to programmatically set the zoom level on the UIWebview?
*I've also tried a QLPreviewController and am noticing this same default zoomed out behaviour
Just had the same trouble with setting the zoomScale and no visible respond in the webView.
My mistake was to try settings in webViewDidFinishLoad:.
The solution is to use viewDidAppear:
-(void)viewDidAppear:(BOOL)animated{
[self.webView.scrollView setZoomScale:1.5 animated:YES];
}
I got there in the end by looping through the webviews subviews and zooming the scrollview:
for (UIView *subView in [self.view subviews]) {
if ([subView isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subView;
[scrollView setZoomScale:2.5f animated:YES];
}
}
Its important to note that you need to wait for the webview to render on the screen before attempting to zoom, this caught me out ;-\
you can use
webView.scalesPageToFit = NO
[webView stringByEvaluatingJavaScriptFromString:#"document. body.style.zoom = 5.0;"];

UITableView in Popover scrolls out of bounds when keyboard is opened

I have a popover that contains a UITableView. This UITableView has a cell with a text field in it:
alt text http://cl.ly/1b50a21ca8202d22db1b/content
When the popover opens near the bottom of the screen, and I tap the text field to edit it, the keyboard comes up, and the popover moves up to avoid being covered by the keyboard. But as it moves up, the table view in the popover scrolls up out of bounds:
alt text http://cl.ly/4fe64fbfe9518f20560d/content
I can scroll it back down, but how do I prevent this from happening.
I've come to the conclusion that this is a bug. I have filed a bug report with Apple (rdar://8156616) as well as a report on OpenRadar.
For anyone interested, here is a sample project that demonstrates the issue.
Try disabling scrolling on the table view.
[self.tableView scrollingEnabled:NO];
How are you setting the content view of your pop over controller. Please try editing the auto-resizing mask of the content view and set it from top-left.
Hope this helps.
Thanks,
Madhup
Perhaps you can set the UITableView's contentSize manually? So that there is no excess for it to scroll up.
I had the same problem but when hiding the keyboard and reloading the table view!
There is one solution for this problem! What you have to do is first hide the keyboard and reload the table view or change the table view in method recieving keyboard did hide notification!
At first I called
[textView resignFirstResponder]; or [textField resignFirstResponder];
and then
-(void)keyboardDidHide:(NSNotification *)notif {
//Check some conditions if you want
[tableView reloadData];
}
You need to listen to the keyboard will show notification:
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardStatusForPopover(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
Then you have to see if the keyboard frame intersects with the popover and subtract the values added from the keyboard to the popover container frame:
#objc func handleKeyboardStatusForPopover(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if keyboardSize.intersects((self.resultPopOverTableViewController!.view.superview!.superview!.frame).insetBy(dx: 0, dy: 60)) {
let popOverContainer = viewControllerDisplayedbyPopover?.view.superview!.superview!
// cellDisplayingDetailInfo = sourceview of popover
popOverContainer!.frame.origin.y = popOverContainer!.frame.minY - keyboardSize.height + cellDisplayingDetailInfo.frame.maxY
}
}
}