iOS Disable Initial Zoom in webview? - ios

I need to Disable zooming for UIWebview iOS for swift 2.2.
I am trying like this:
self.webView.scrollView.maximumZoomScale = 1.0;
self.webView.scrollView.minimumZoomScale = 1.0;
But this is not helping. Is there any other way I can disable zoom while the page loads for the first time?

This works for me:
let scrollView = webView.subviews.objectAtIndex(0)
scrollView.delegate = self
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return someView
}
UPDATE: For iOS 5 or higher get scrollView like this:
webview.scrollView.delegate = self;
You can try this also:
use 'webView.scrollView.zoomScale' instead of 1.0, and do it inside the 'webViewDidFinishLoad' (remember to set the web view delegate)

Turn off scalesPageToFit for the webView and set the zoom level manually for the first time loading. SO Post
Hope this helps.

Related

Hide "Show More" button from Today widget in iOS10

I am building an iOS Today widget, and while testing for iOS 10, I see a "Show More" / "Show Less" button on the top right of the widget header. How can I remove this button? I am using Objective-C.
In iOS 10, as far as I know, the show more option is new and we cannot remove it, but we can modify it as needed.
The following code will allow you to automatically size the Today widget. Just change the table or collection view or whatever you used in your project.
static CGFloat padding = 25.0;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// This will remove extra separators from tableview
self.articleTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
// Add the iOS 10 Show More ability
[self.extensionContext setWidgetLargestAvailableDisplayMode:NCWidgetDisplayModeExpanded];
}
- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize {
if (activeDisplayMode == NCWidgetDisplayModeCompact){
// Changed to compact mode
self.preferredContentSize = maxSize;
}
else{
// Changed to expanded mode
self.preferredContentSize = CGSizeMake(self.articleTableView.contentSize.width, self.articleTableView.contentSize.height + padding);
}
}
In viewDidLoad you can set the largest available display mode.
[self.extensionContext setWidgetLargestAvailableDisplayMode:NCWidgetDisplayModeCompact];
This will remove the Show More/Less button, but it may not be what you want. The maximum allowed size for the compact view is fairly small.
You can implement:
-(void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode withMaximumSize:(CGSize)maxSize
to update your preferredContentSize. The maxSize parameter will be the maximum allowed size for the activeDisplayMode.
-[NCWidgetProviding widgetActiveDisplayModeDidChange:withMaximumSize:]
Is probably what you're looking for, I would reference this
Sadly you cannot hide it and should conform to the
widgetActiveDisplayModeDidChange:withMaximumSize:
widgets that doesn't show this control were not build for iOS10
I know the original post mentions using objective-c
but in the event anyone needs the swift answer, here it is
override func viewDidLoad()
{
super.viewDidLoad()
self.extensionContext?.widgetLargestAvailableDisplayMode = .compact
}
When set to compact, the app will only support compact mode i.e. show less/show show buttons/functionality will be gone.
here's some documentation for more info
Placing this line of code inside the widgetActiveDisplayModeDidChange delegate method solved my problem.
[self.extensionContext setWidgetLargestAvailableDisplayMode:NCWidgetDisplayModeExpanded];
If you wanna hide the show more/ show less option replace NCWidgetDisplayModeExpanded with NCWidgetDisplayModeCompact.
- (void)widgetActiveDisplayModeDidChange:(NCWidgetDisplayMode)activeDisplayMode
withMaximumSize:(CGSize)maxSize {
[self.extensionContext setWidgetLargestAvailableDisplayMode:NCWidgetDisplayModeExpanded];
}

hide the inputToolbar in JSQMessagesViewController

I am using JSQMessagesViewController for my chat application. When there is no internet activity I would like to hide the inputToolbar
I tried this, but that does not work:
self.inputToolbar.frame.size = CGSize(width: 0,height: 0)
When I set this, then for less than a second it's gone:
self.inputToolbar.preferredDefaultHeight = 0
Any idea how to do this?
Maybe disabling the inputToolbar could also be good enough.
I found a better solution which doesn't have any side effects.
You can make the actions in a descendant class of JSQMessagesViewController.
1. Make this method of base class available for you:
#interface JSQMessagesViewController ()
- (void)jsq_setCollectionViewInsetsTopValue:(CGFloat)top
bottomValue:(CGFloat)bottom;
#end
2. Override parent realization of method (called when size changed):
- (void)jsq_updateCollectionViewInsets {
CGFloat topInset = self.topLayoutGuide.length + self.topContentAdditionalInset;
CGFloat bottomInset = 0.0;
[self jsq_setCollectionViewInsetsTopValue:topInset bottomValue:bottomInset];
}
3. Write the method to hide input toolbar forever:
- (void)hideInputToolbar {
self.inputToolbar.hidden = YES;
[self jsq_updateCollectionViewInsets];
}
4. Enjoy!
Instead of removing from superview and having to add back as a subview, why not just use:
[self.inputToolbar setHidden:YES];
It turned out that this will work:
override func viewDidLoad() {
super.viewDidLoad()
self.inputToolbar.removeFromSuperview()
}

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.

Transparent background for WKWebView

In my app I wish to make the WKWebView with transparent background, so the view behind (an ImageView) can show through as background picture.
webView!.opaque = false
webView!.backgroundColor = UIColor.clearColor()
The code above works fine with UIWebView, but if webView is WKWebView, a white background will be shown.
Also tried putting transparent background color in CSS. The result is the same, only works in UIWebView but not WKWebView. Any suggestion?
For iOS 10+ using Swift:
self.webView = WKWebView()
self.webView!.isOpaque = false
self.webView!.backgroundColor = UIColor.clear
self.webView!.scrollView.backgroundColor = UIColor.clear
This code might help you. Updated for Swift 3+
self.webView = WKWebView()
self.webView.isOpaque = false
self.webView.backgroundColor = UIColor.clear
self.webView.scrollView.backgroundColor = UIColor.clear
using objective c.
wkWebView.backgroundColor = [UIColor clearColor];
wkWebView.scrollView.backgroundColor = [UIColor clearColor];
wkWebView.opaque = false;
It will remove the white background colour in wkwebView.
This bug seems to be fixed in Beta 5.
I know this is a very old question. But I've been struggling with this today. I don't know if it's just me, but webView.backgroundColor was undefined in WKWebView, and webView.opaque was read-only. The only way for me to fix this was to set the web views layer background color to the CGColor clear
webview.wantsLayer = true
webView.layer?.backgroundColor = NSColor.clearColor().CGColor
and the containing NSView the same way
webViewParent.layer?.backgroundColor = NSColor.clearColor().CGColor
That's the only thing that worked for me, I hope it could help someone else as well.
WKWebView for macOS with Objective-C
This is the magic line for transparent WKWebView views within macOS.
[webView setValue:[NSNumber numberWithBool: YES] forKey:#"drawsTransparentBackground"];
Uncheck Opaque checkbox in Interface Builder
Update July 2021, Xcode 13
Looks like the checkbox is still there
Code below works for me:
[wkWebView setValue:YES forKey:#"drawsTransparentBackground"];
Haven't worked with WKWebView yet but even UIWebView used to have this issue and the solution was to wrap the html content inside something like this:
<body style="background-color:transparent;"></body>
Hope it helps.
If you're loading a PDF and want a background color different than the standard gray, it seems that it is necessary to wait until the document is loaded, then clear the backgrounds of the subviews. The advantage of using a WKWebView over a PDFView (iOS 11+) is that WKWebViews have double-tap to zoom and a page count indicator built in, and are compatible with older versions of iOS.
It should be noted that it's not a great practice to dig into system views like this as Apple can change the implementation at any time, potentially breaking the solution.
Here is how I implemented a PDF preview controller with a black background in Swift 4:
class SomeViewController: UIViewController {
var observer: NSKeyValueObservation?
var url: URL
init(url: URL) {
self.url = url
super.init(nibName: nil, bundle: nil)
}
func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.black
let webView = WKWebView()
webView.translatesAutoResizingMaskIntoConstraints = false
self.view.addSubview(webView)
NSLayoutConstraint.activate([
webView.topAnchor.constraint(equalTo: self.view.topAnchor),
webView.leftAnchor.constraint(equalTo: self.view.leftAnchor),
webView.rightAnchor.constraint(equalTo: self.view.rightAnchor),
webView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
self.observer = webView.observe(\.isLoading, changeHandler: { (webView, change) in
webView.clearBackgrounds()
})
webView.loadFileURL(self.url, allowingReadAccessTo: self.url)
}
}
extension UIView {
func clearBackgrounds() {
self.backgroundColor = UIColor.clear
for subview in self.subviews {
subview.clearBackgrounds()
}
}
}
I don't know if it can help, but i used this solution below:
func setBackgroundWhite(subviews: [UIView]) {
for subview in subviews {
subview.backgroundColor = .white
setBackgroundWhite(subviews: subview.subviews)
}
}
This does recursive call inner in subviews, setting up the background to white, or another color.
I used this code when loading PDF files, WKWebView and iOS 10+
In my case I need to change background-color to transparent in Styles and delete an App from phone/simulator manualny - it looks that WKWebView Cache HTML / Styles

Disabling zoom-in zoom-out functionality of the UIWebView

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,..

Resources