Scaling content in WKWebView - swift

I recently started working on WKWebView and have minimum knowledge about it.
I want to know how to scale the content in the WKWebView so that it fits into the given view size without scrolling.
I have tried setting the height of the view to be screen size and load some large html content to the WKWebView and disable scrolling. Then only a part of the content is shown.
Then I searched for the approach to scale the content of the WKWebView so that the entire content is seen without need for scrolling.
Many people have posted questions regarding this but some questions were still unanswered and some other solutions did not work for me.

For the WKWebView try the following three options depending upon your requirement:
webview being the webview in your file
webView.contentMode = .scaleAspectFill
or
webView.contentMode = .scaleAspectFit
or
webView.contentMode = .scaleToFill

Given a webView object of type WKWebView you can do
webView.contentMode = .scaleToFill

This 'contentMode' will work after set webview frame correctly (need to fit to the device).
var webView: WKWebView!
func setupView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: view.bounds.size.height), configuration: webConfiguration)
webView.uiDelegate = self
webView.contentMode = .scaleAspectFit
webView.sizeToFit()
webView.autoresizesSubviews = true
self.view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
setupView()
}

Related

UIScrollview constraints for different devices

Currently, I have the following codes to make UIScrollView work for iPhone that I have set it up in the main storyboard.
var images: [String] = ["image1", "image2"]
var frame = CGRect(x:0,y:0,width:0,height:0)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
pageControl.numberOfPages = images.count
for index in 0..<images.count {
frame.origin.x = scrollView.frame.size.width * CGFloat(index)
frame.size = scrollView.frame.size
let imgView = UIImageView(frame: frame)
imgView.image = UIImage(named: images[index])
self.scrollView.addSubview(imgView)
}
scrollView.contentSize = CGSize(width: (scrollView.frame.size.width * CGFloat(images.count)), height: scrollView.frame.size.height)
scrollView.delegate = self
}
I am trying to make a horizontal scrollview in xcode but when I run the project on iPad, the scrollview changes to the screen size but not the images. I looked up solutions online by adding views to the scroll view but it does not work. I am trying to make the images resize accordingly to the scrollview size.
Quick fact: Images are stored in Assets.xcassets.
Any help would be very much appreciated. Thanks!

Programmatically place partial image over another in UIView using Swift 3

I just started working on Swift last week and i need a suggestion if the following approach is right ways of laying partial image on top of another image.
I have a UIView in which I am creating 3 images programmatically. Left arrow image, middle mobile image and right arrow image as shown below. Can I partially place arrow images 50% on the mobile image?
I have tried:
func setupUI(){
let mobileImage = UIImage(named: "mobile")
let arrowImage = UIImage(named: "arrow")
middleView = UIImageView(frame: CGRect(x: arrowImage!.size.width/2, y:0 , width:mobileImage!.size.width, height:mobileImage!.size.height))
middleView.image = mobileImage
middleView.layer.borderWidth = 1.0
middleView.layer.cornerRadius = 5.0
self.addSubview(middleView)
let yForArrow = mobileImage!.size.height - arrowImage!.size.height
leftArrow = UIImageView(frame: CGRect(x: 0, y:yForArrow, width:arrowImage!.size.width, height:arrowImage!.size.height))
leftArrow.image = arrowImage
self.addSubview(leftArrow)
let rightArrowX = mobileImage!.size.width
rightView = UIImageView(frame: CGRect(x: rightArrowX, y:yForArrow, width:arrowImage!.size.width, height:arrowImage!.size.height))
rightView.image = arrowImage
self.addSubview(rightView)
}
*At start it was not working, as i forgot to add setupUI() in init method. As shown in answer bellow.
Is setting frame correct way of doing it OR i should be using constraints?
To me it looks bad approach as i am hard coding the numbers in CGRect.
*This image is created in MS paint to show what it should look on iPhone.
I found the problem i missed adding setupUI() in init method.
SetupUI programatically adds images on UIView. As it was missing so no image was appearing in the iPhone simulator.
override init(frame: CGRect) {
super.init(frame: frame)
setupUI() // Code to add images in UIView
}

Resize WebView for ipad swift 3

I have a First View, where when I click on one button, it opens an UIWebView,
I want this webView to take only 3/4 of my view when I look at it from an Ipad, if it's an Iphone I want to get a full screen view.
Here is my code :
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
I try the method init with arbitrary values frame: .init(x: 20, y: 20, width: 20, height: 20)
But it's still taking full screen, for detect if it's an Ipad I use this condition
if (UIScreen.main.traitCollection.userInterfaceIdiom.rawValue == 1)
{
// if here I'm an Ipad I can resize my webview
webView.frame.size.height = view.frame.height - // some number
}
Isn't this exactly what adaptive user interfaces and size classes in Interface Builder is for? Have a look at this WWDC video from 2014.

UIImageView and UIWebView in UIScrollView

I have added UIImageView with view frame height and width and below that UIWebView has to be loaded. I am not sure how to implement this. I tried to add both in UIScrollView. Image is perfectly displaying but WebView not loaded. Adding UIWebView to UIScrollView is not good practice as its below screen, I need to make it in scroll. I am not using Storyboard, all through programming.
Please suggest what should be the best approach to implement this. Wether to use UIWebView in UIScrollView or if added then why it's not loaded. It comes blank.
func viewDidLoad(){
var image = UIImage(frame: view.bounds)
var webView = UIWebView(frame: CGRect(x: 0, y: self.view.frame.height, width: self.view.frame.width, height: self.view.frame.height))
var scrollView = UIScrollView()
scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height*2)
webView.delegate = self
webView.loadRequest(URLRequest(url: URL(string: "http://www.google.com")))
scrollView.addSubview(image)
scrollView.addSubView(webView)
self.view.addSubview(scrollView)
}
func webViewDidFinishLoad(_ webView: UIWebView){
}
func webViewDidStartLoad(_ webView: UIWebView){
}
Thanks
Your code is fine.
Please check if you are getting this error:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
If you are getting this error, then add this in info.plist file:

Keeping video playing within the webView instead of presenting fullscreen?

I know in iOS 10 you can pinch the video player to keep the video playing within the webview. Is there any way to set the webview so that the video player doesn't open in full screen on default?
webView.allowsInlineMediaPlayback = true
is what I believe you are looking for
The following is working for in WKWebView in swift 4.1
The main part of the WKWebView in WKwebviewConfiguration
wkwebView.navigationDelegate = self
wkwebView.allowsBackForwardNavigationGestures = true
self.wkwebView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
let config = WKWebViewConfiguration()
config.allowsInlineMediaPlayback = true
wkwebView = WKWebView(frame: wkwebView.frame, configuration: config)
self.view.addSubview(wkwebView)
self.wkwebView.load(NSURLRequest(url: URL(string: self.getUrl())!) as URLRequest)