I'm quite new to Swift, I just want to show a mobile version of a site using Apple's WKWebView but on iPhone X or later where there's a notch, the status bar is not solid which doesn't look good if the website has a sticky navigation bar.
I've tried everything I could find online but nothing worked, so I thought I might post my issue here.
I would like to make the status bar solid white so it matches the sticky navigation bar.
This is my code:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override var prefersStatusBarHidden: Bool {
return true
}
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string:"https://www.webthat.nl")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}
}
This is what I get on iPhones with a notch:
Try adding this code
override func viewWillAppear(_ animated: Bool) {
self.navigationItem.hidesBackButton = true
let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
statusBar.backgroundColor = UIColor.white
}
}
Also add this to info.plist file
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
Related
I try to show a full responsive bootstrap website in a webView app. The app should just load my website as if I load the website in the browser of the smartphone/tablet.
However, the app looks like this on tablets:
How can I make the app full responsive?
Whole Code:
ViewController.swift
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
// 1 The webView loads the url using an URLRequest object.
let url = URL(string: "https://www.blizz-z.de/")!
webView.load(URLRequest(url: url))
// 2 A refresh item is added to the toolbar which will refresh the current webpage.
let refresh = UIBarButtonItem(
barButtonSystemItem: .refresh,
target: webView,
action: #selector(webView.reload)
)
toolbarItems = [refresh]
navigationController?.isToolbarHidden = true
navigationController?.isNavigationBarHidden = true
}
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
let color = UIColor.black;
self.view.backgroundColor = color
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
There is no problem in your code.
Switch to a target settings in Xcode, on a tab "General" in a section "Deployment target" switch "Devices" to "Universal".
Also add "Launch Screen.storyboard" if you did not add it.
you should add constraint for the webView, and the constant of top/bottom/left/right constraint is 0 to superView, not safe area
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.setupWebview()
let refresh = UIBarButtonItem(
barButtonSystemItem: .refresh,
target: webView,
action: #selector(webView.reload)
)
toolbarItems = [refresh]
navigationController?.isToolbarHidden = true
navigationController?.isNavigationBarHidden = true
}
func setupWebview(){
self.webView.frame = UIScreen.main.bounds
self.webView.navigationDelegate = self
self.view.backgroundColor = UIColor.black
let url = URL(string: "https://www.blizz-z.de/")!
self.webView.load(URLRequest(url: url))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
title = webView.title
}
}
Try this... (Note: Not a copy pasted code)
Please excuse my ignorance. I am very new to iOS development and Xcode. I have tried searching many pages on this site and haven't found any that directly solve my issue. Any help you can provide would be greatly appreciated!
I am unable to get my webview (WKWebview) to remain within the Safe Area boundaries (I think that's the best way to describe it). I have autogenerated constraints set on the webview and I can see within the Main.Storyboard editor that the webview is within the safe area.
Unfortunately the webview ignores these boundaries and encompasses the entire view and the text on my webpage appears at the top of the display behind the time, battery and connection icons.
What am I doing wrong?
Here is my code and a few pictures:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let url = Bundle.main.url(forResource: "bulk_material_table", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
webView.load(request)
}
}
Your constraints are OK. The problem is with your overridden loadView() method. You don't need to create a new WKWebView since you are using storyboards and Interface Builder is creating it. Remove this method and move uiDelegate to viewDidLoad():
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
#IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView.uiDelegate = self // Move delegate here
let url = Bundle.main.url(forResource: "bulk_material_table", withExtension: "html")!
webView.loadFileURL(url, allowingReadAccessTo: url)
let request = URLRequest(url: url)
webView.load(request)
}
}
Tried to make this work programmatically. Is it OK? Does anybody know how to change the color of top inset of safe area?
var webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(webView)
guard let url = URL(string: "https://apple.com/") else {
return
}
webView.load(URLRequest(url: url))
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// webView.frame = view.bounds // previously
let topPadding = view.safeAreaInsets.top
webView.frame = view.frame.inset(by: UIEdgeInsets(top: topPadding, left: CGFloat(0), bottom: CGFloat(0), right: CGFloat(0)))
}
it doesn't keep WKWebView inside safe area before and here what I've thought to add
view.insetsLayoutMarginsFromSafeArea = true
I'm trying to show a website using the WKWebView, it works fine as for the internet work.
problem is it is loading the site twice (it seems like it's going to another ViewController).
here is my code:
#IBOutlet weak var myWebView: WKWebView!
override func loadView() {
myWebView = WKWebView()
myWebView.navigationDelegate = self as? WKNavigationDelegate
view = myWebView
}
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = false
let url = URL(string: "https://google.com")!
myWebView.load(URLRequest(url: url))
}
I am using iOS9 and XCode7.2 to make my app display a webpage using WkWebView and Swift.
To eliminate possible errors, I have created an XCode project that has only the following code and there is nothing in Main.storyboard.
The weird thing is that the webpage only shows up "1 time" after I ran the code over and over the past few days. Two members on StackOverflow confirmed that the code works for XCode 8 / Swift 3, and Xcode 7.3.1 / iOS 9.3 simulator.
Is my xCode too old or what other problems might cause it?
import UIKit
import WebKit
class WebViewController: UIViewController {
#IBOutlet var containerView : UIView!
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView!
}
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://www.yahoo.com")!
let req = NSURLRequest(URL: url)
self.webView!.loadRequest(req)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Step : 1 Import webkit in ViewController.swift
import WebKit
Step : 2 Declare variable of webView.
var webView : WKWebView!
Step : 3 Adding Delegate of WKNavigationDelegate
class ViewController: UIViewController , WKNavigationDelegate{
Step : 4 Adding code in ViewDidLoad.
let myBlog = "https://www.google.com/"
let url = NSURL(string: myBlog)
let request = NSURLRequest(URL: url!)
// init and load request in webview.
webView = WKWebView(frame: self.view.frame)
webView.navigationDelegate = self
webView.loadRequest(request)
self.view.addSubview(webView)
self.view.sendSubviewToBack(webView)
The correct way with auto layout for all device:
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
setupWebView()
}
fileprivate func setupWebView() {
webView.uiDelegate = self
webView.translatesAutoresizingMaskIntoConstraints = false
DispatchQueue.main.async {
guard let url = URL(string: "http://www.apple.com") else { return }
self.webView.load(URLRequest(url: url))
}
view.addSubview(webView)
webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
webView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
webView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
}}
in info.plist:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
i'm new to the Swift programming language!
I'm building a simple app that shows a webpage, my problems is, when i rotate the device, the WebView isn't sized to fill the Scene...
I've tried to use this code:
func resizeWebView(){
webView.scalesPageToFit = true
webView.contentMode = UIViewContentMode.ScaleToFill
webView.multipleTouchEnabled = true
webView.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight
self.view.addSubview(webView)
}
i call this function on the viewDidLoad() function, is that right?
Are there anyway to do this? Everything that i found on the internet is for Objective-C...
Thanks :)
I just did a simple example:
import UIKit
import WebKit
class ViewController: UIViewController {
#IBOutlet var containerView : UIView?
var webView: WKWebView?
override func loadView() {
super.loadView()
self.webView = WKWebView()
self.view = self.webView!
}
override func viewDidLoad() {
super.viewDidLoad()
var url = NSURL(string:"http://chart.finance.yahoo.com/z?s=AAPL&t=6m&q=l&l=on&z=s&p=m50,m200")
var req = NSURLRequest(URL:url)
self.webView!.loadRequest(req)
}
}
I did not use auto layout or deal with status bar issues, but no problem resizing on rotation.