UIScrollView not working in swift 3 - ios

I have made a baseViewController which is derived from UIViewController, SlideMenuDelegate and UIWebViewDelegate
Then I have created subclass which are derived from baseviewcontroller
LoginVC - displays login screen
PlayVC - shows a webview if login is successful.
Everything is fine except that webview is not scrolling at all.
I have tried everything suggested in all posts so far regarding scrolling
Below is my code written in PlayVC.swift
#IBOutlet weak var web1: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
addSlideMenuButton()
let webUrl : URL = URL(string: "http://brainyknights.com")!
let webRequest : URLRequest = URLRequest(url: webUrl)
web1.scalesPageToFit = true
web1.isUserInteractionEnabled = true
web1.scrollView.isScrollEnabled = true
web1.loadRequest(webRequest)
// Do any additional setup after loading the view.
}

remove this line web1.scalesPageToFit = true
#IBOutlet weak var web1: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
addSlideMenuButton()
let webUrl : URL = URL(string: "http://brainyknights.com")!
let webRequest : URLRequest = URLRequest(url: webUrl)
//web1.scalesPageToFit = true
web1.isUserInteractionEnabled = true
web1.scrollView.isScrollEnabled = true
web1.loadRequest(webRequest)
// Do any additional setup after loading the view.
}

According Apple Documentation
scales​Page​To​Fit
A Boolean value determining whether the webpage scales to fit the view
and the user can change the scale.
if web1.scalesPageToFit is true then the web content will fit in your UIWebView frame and the contentSize will have the same height and width as your UIWebViewframe making imposible to be scrollable
so remove this line and must work
web1.scalesPageToFit = true
I hope this helps you

Related

How to make transparent background WKWebView in Swift

I want a transparent web page in swift, so I have tried the below code according to this answer. Still, I am not getting a transparent web page. nothing changes in webview colour.. may I know why??
where am I going wrong? please help me in below code.
Total code:
import UIKit
import WebKit
class WebviewViewController: UIViewController {
#IBOutlet weak var testWebView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "https://developer.apple.com/swift/") else { return }
let request = URLRequest(url: url)
testWebView.load(request)
// Do any additional setup after loading the view.
self.testWebView = WKWebView()
self.testWebView!.isOpaque = false
self.testWebView!.backgroundColor = UIColor.clear
self.testWebView!.scrollView.backgroundColor = UIColor.clear
}
}
Please help me with the code.
the code to make transparent background is as follow what you already added.
self.testWebView!.backgroundColor = UIColor.clear
now question is you added right code already then why you are not getting reliable output ..?
Also , if you try
self.testWebView!.alpha with any value, it will affect all of WebPages as WkWebView is a single view and changing it's alpha will also affect the components within...
it happened because the page you load in WebViewController has some HTML and CSS code, you make your WebViewController transparent but because of that HTML &CSS you can't see it's transparency as each webpage has it's own background color settings (which is merely impossible to change for each webpage)
I hope you will understand and it will help you ...:)
as I see, you use storyboard (#IBOutlet) and you can use Storyboard for setting your WKWebView:
And about code. This is enough for the result. You shouldn't set again self.testWebView = WKWebView(), because you use storyboard and you can set isOpaque with storyboard. As result:
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.navigationDelegate = self
let url = URL(string: "https://developer.apple.com/swift/")!
webView.load(URLRequest(url: url))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
let js = "(function() { document.body.style.background='transparent'; })();"
webView.evaluateJavaScript(js) { (_, error) in
print(error)
}
}
}
and evaluateJavaScript helped to add transparency for background:

video will not load inline

I want my video to stream in the view not takeover the screen, ive specified that every way, toggled the option in storyboards and still nothing. any thoughts or ideas, maybe something im missing, please feel free to test the code your self and see the result (fills the entire screen, and still unable to play inline.)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
webConfiguration.mediaTypesRequiringUserActionForPlayback = []
LiveStream = WKWebView(frame: CGRect(x: 0, y: 0, width: 375, height: 300), configuration: webConfiguration)
self.view.addSubview(LiveStream)
if let videoURL:URL = URL(string: "https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8?playsinline=1") {
let request:URLRequest = URLRequest(url: videoURL)
LiveStream.load(request)
}
Edited the link to a 24/7 uptime (https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8?playsinline=1)
I am seeing some mistakes here.
First of all you have already added WKWebView in your storyboard and I am guessing that from your
#IBOutlet var LiveStream: WKWebView!
and you are also adding it into your view again with
self.view.addSubview(LiveStream)
Which is not correct way to add it.
You can use UIView for that.
For that add a UIView in your storyboard and create IBOutlet for that
#IBOutlet weak var viewForEmbeddingWebView: UIView!
then declare an instance var LiveStream: WKWebView!
Now you can configure LiveStream as shown below:
let webConfiguration = WKWebViewConfiguration()
webConfiguration.allowsInlineMediaPlayback = true
webConfiguration.mediaTypesRequiringUserActionForPlayback = []
LiveStream = WKWebView(frame: viewForEmbeddingWebView.frame, configuration: webConfiguration)
self.viewForEmbeddingWebView.addSubview(LiveStream)
if let videoURL:URL = URL(string: "https://www.youtube.com/embed/9n1e1N0Sa9k?playsinline=1") {
let request:URLRequest = URLRequest(url: videoURL)
LiveStream.load(request)
}
And your result will be:
As you have noticed video is playing inside the WKWebView not in full screen.
Note:
Your URL wasn't working for me so I have used another URL for demonstrate.

How do I make my ImageView scrollable without affecting the rest of the view?

How Do I add multipleImages for the top part and have it scrollable without moving the rest of the content? Also, how do I have the little dots at the bottom to indicate which image I'm on? My code for the image is down below.
import SDWebImage
#IBOutlet weak var headerImage: UIImageView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let imageUrl:NSURL? = NSURL(string: headerPhoto!)
if let url = imageUrl {
headerImage.sd_setImage(with: url as URL!)
}
}
You are looking for something called image slide show or image slider.
Instead of creating them by yourself which requires lots of effort, here is a GitHub library that is easy to use.
Basicly the way slider works is that it is a horizontal scroll view and each cell in the scroll view is an image so that you can scroll. Then, put a fixed page index element on the bottom of the scroll view to tell you which image you are currently at.
To use it, create a UIView in your viewcontroller and set both class and module to ImageSlideshow. Then connect it to your ViewController.swift as IBOutLet.
Then create an array of image urls
let alamofireSource = [AlamofireSource(urlString: "imgurl1")!, AlamofireSource(urlString: "imgurl2")!, AlamofireSource(urlString: "imgurl3")!]
And finally in your viewDidLoad() function:
override func viewDidLoad() {
super.viewDidLoad()
slideshow.backgroundColor = UIColor.white
slideshow.slideshowInterval = 5.0
slideshow.pageControlPosition = PageControlPosition.underScrollView
slideshow.pageControl.currentPageIndicatorTintColor = UIColor.lightGray
slideshow.pageControl.pageIndicatorTintColor = UIColor.black
slideshow.contentScaleMode = UIViewContentMode.scaleAspectFill
slideshow.setImageInputs(alamofireSource)
}

UIWebView Delegate not changing other views

I have a webView in a simple application which is under a UIImageView. I intend on displaying the UIImageView until webView loads some data and fires a method in the web view delegate. Webview loads the data, delegate method is called fine. However, I am having trouble manipulating other views like the UIImageView.
Here is my code;
class ViewController: UIViewController,UIWebViewDelegate {
//contains the imageView and activityWorkingIndicator
#IBOutlet weak var splash_view: UIView!
#IBOutlet var contentWebView: UIWebView!
//Contains the Webview and other views to be displayed after the splash his hidden
#IBOutlet weak var splash_view_not: UIView!
override func viewWillAppear(animated: Bool)
{
contentWebView?.scrollView.bounces = false;
contentWebView?.delegate = self
let url = NSURL(string: "http://localhost/something/");
let request = NSURLRequest(URL: url!);
contentWebView?.loadRequest(request);
}
....
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
if let scheme = request.URL?.scheme {
if scheme == "myRequestScheme"{
let task : String = (request.URL?.host)!;
switch task {
case "systemReady":
print("checkpoint 1");//works fine
splash_view?.hidden = true;//no effect at all
splash_view_not?.hidden = false;//no effect at all
print("checkpoint 2");//works fine
break;
default:
break
}
}
}
return true;
}
}
What am I missing here?
It looks like you are using Auto Layout. If that is the case, using .hidden has more implications than you think. If you are calling it in a method after the view has already been loaded, you need to work with constraints instead of hidden. Some people create constraint outlets and set them to active to hide things. Some people set alphas and bring subviews to front to hide things, etc.
I believe this is the preferred way:
http://candycode.io/hiding-views-with-auto-layout/
And here is a stack overflow thread with A LOT of information on your best options to hide things with AutoLayout:
AutoLayout with hidden UIViews?
You should hide or show any control in delegate method on main thread.
Use like this
DispatchQueue.main.async {
splash_view_not?.hidden = false
}

UIWebView webViewDidLoadFinish method not called

I've been playing around with web views in swift this evening, but have run into a bit of an issue.
For some reason I'm not able to get the webViewDidStartLoad or webViewDidFinishLoad methods to fire.
In my storyboard, I have an outlet called webView linked to my UIWebView element.
Is anyone able to help me with what I am doing wrong?
This is my viewController.swift file:
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
#IBOutlet var webView : UIWebView
var url = NSURL(string: "http://google.com")
override func viewDidLoad() {
super.viewDidLoad()
//load initial URL
var req = NSURLRequest(URL : url)
webView.loadRequest(req)
}
func webViewDidStartLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = true
println("AA")
}
func webViewDidFinishLoad(webView : UIWebView) {
//UIApplication.sharedApplication().networkActivityIndicatorVisible = false
println("BB")
}
}
Try this!
var req = NSURLRequest(URL: url)
webView.delegate = self
webView.loadRequest(req)
I experienced the same issue, even I did confirmed the UIWebViewDelete to self and implemented its methods.
//Document file url
var docUrl = NSURL(string: "https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0ahUKEwjjwPSnoKfNAhXFRo8KHf6ACGYQFggbMAA&url=http%3A%2F%2Fwww.snee.com%2Fxml%2Fxslt%2Fsample.doc&usg=AFQjCNGG4FxPqcT8RXiIRHcLTu0yYDErdQ&sig2=ejeAlBgIZG5B6W-tS1VrQA&bvm=bv.124272578,d.c2I&cad=rja")
let req = NSURLRequest(URL: docUrl!)
webView.delegate = self
//here is the sole part
webView.scalesPageToFit = true
webView.contentMode = .ScaleAspectFit
webView.loadRequest(req)
above logic worked perfectly with test URl I got from quick google search.
But I when I replaced with mine. webViewDidFinishLoad never get called.
then How we solved?
On backed side we had to define content-type as document in headers. and it works like charm.
So please make sure on your server back-end side as well.
Here's my 2 cents battling with the same problem in SWIFT 3:
class HintViewController: UIViewController, UIWebViewDelegate
Declare the delegate methods this way (note the declaration of the arguments):
func webViewDidStartLoad(_ webView: UIWebView)
func webViewDidFinishLoad(_ webView: UIWebView)
Remember to set self to the webview's delegate property either in Interface Builder (Select the webview, drag from the delegate outlet to the webview from the Connections Inspector OR programmatically: self.webview.delegate = self)
As others noted, setting the delegate of UIWebView and conforming to the UIWebViewDelegate protocol is the best possible solution out there.
For other's who might make it here. I had put my delegate methods in a private extension which couldn't be accessed by the delegate caller. Once I changed the extension to internal the delegates started to get called properly.
There is my detailed solution for Swift 3:
1) in class declaration write the UIWebViewDelegate. For example:
class MyViewController: UIViewController, UIWebViewDelegate {
2) of course in storyboard make link to your UIViewController like this:
#IBOutlet weak var webView: UIWebView!
3) in the func viewDidLoad add one line:
self.webView.delegate = self
Nothing more. Special thinks to LinusGeffarth and LLIAJLbHOu for idea.

Resources