UIImageView and UIWebView in UIScrollView - swift

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:

Related

how to resize a view in live playgrounds ?? Xcode

I am planning to create a live playground for coming wwdc scholarships but I have no experience in creating live playgrounds because I have worked with single view apps so far
I actually don't know what size should be the view in my playground and I am not able to resize it can any one tell me how to do that , I have tried the below code but it gives me error and doesn't work please help me and any advice related to wwdc project is appreciated thank you
let main = viewController()
main.view.frame.size = CGSize(width: 300, height: 600)
this code gives error:= expression failed to parse, unknown error
Please tell me how to resize the view and also how should I do it ??
You should change entire frame or bounds properties to resize the view:
import UIKit
import PlaygroundSupport
class ViewController : UIViewController {
...
}
let viewController = ViewController()
viewController.view.frame = CGRect(x: 0, y: 0, width: 300, height: 600)
PlaygroundPage.current.liveView = viewController
PlaygroundPage.current.needsIndefiniteExecution = true
When you use navbar it is important to size the NavigationController and not the ViewController.
Like this:
let vc = MyViewController()
let navBar = UINavigationController(rootViewController: vc)
navBar.view.frame = CGRect(x: 0, y: 0, width: 320, height: 568)
PlaygroundPage.current.liveView = navBar

How to make a stretchable header view collection view [Swift]

In my swift app I've a collection view and I want to creare a stretchable header view like this in table view: https://medium.com/if-let-swift-programming/how-to-create-a-stretchable-tableviewheader-in-ios-ee9ed049aba3
You already answered your question yourself with that article link, unless I miss something.
I will copy & paste for you and others that may have the same question, if it helps, because it even ships with a github link (kudos to Abhimuralidharan # Medium):
Create a tableview with the basic datasource and delegate methods which are required to simply load the table with some data.
Set the tableview’s contentInset property:
tableView.contentInset = UIEdgeInsetsMake(300, 0, 0, 0)
Here, I set the top value as 300 which is a calculated number which I will set as the initial normal height for the header imageview. Now, that we set the contentInset , the tableview’s frame will start at (0,0) and the first cell will start at (0,300).
Now, create an imageview with height 300 and add it to the current View above the tableview.
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 300)
imageView.image = UIImage.init(named: “poster”)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
Then, add the following code in the scrollview delegate method scrollViewDidScroll which gets called every time the tableview is scrolled.
func scrollViewDidScroll (_ scrollView: UIScrollView) {
let y = 300 — (scrollView.contentOffset.y + 300)
let height = min(max(y, 60), 400)
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height)
}
Compile and run the code. Full source code is available in github.

Scaling content in WKWebView

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()
}

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.

Can I add a UIImageView to an UIInputViewController in Swift?

I'm working on a custom keyboard and want to add some UIImageViews to the interface. I can load a .Xib fine as shown below, but when I try to create a UIImage within the main view it causes it to crash. Do you know why this is?
import UIKit
class KeyboardViewController: UIInputViewController {
override func updateViewConstraints() {
super.updateViewConstraints()
}
override func viewDidLoad() {
super.viewDidLoad()
let myView = NSBundle.mainBundle().loadNibNamed("KeyboardView", owner: nil, options: nil)[0] as UIView
self.view.addSubview(myView)
let image = UIImage(named: "grid.png")
let imageSize = CGSize(width: 216, height: 216)
var gridImageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: imageSize))
gridImageView.image = image
//tried this- Crashes
self.addSubview(gridImageView)
//also tried this- Crashes
self.view.insertSubview(view: gridImageView, aboveSubview: myView)
}
If I manually add a ImageView to the .XIB, it also crashes. I feel like UIInputViewController will not let you use a Image View, even though the documentation would indicate it's possible. I'm usually an idiot with these things so am probably misunderstanding the docs:
https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIInputViewController_Class/index.html#//apple_ref/occ/instp/UIInputViewController/inputView