I would like to create a map view with a different style, such as MapBox Dark. However I couldn't get anything but a white blank view with the mapbox logo at the bottom (please see the attached picture)
Which part is wrong in my code?
import UIKit
import Mapbox
class ViewController: UIViewController {
var mapView: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
let styleDarkURL = URL(fileURLWithPath: "mapbox://styles/mapbox/dark-v9")
mapView = MGLMapView(frame: view.bounds, styleURL: styleDarkURL)
mapView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
view.addSubview(mapView)
}
}
You need to replace your
URL(fileURLWithPath: "mapbox://styles/mapbox/dark-v9")
with
URL(string: "mapbox://styles/mapbox/dark-v9")
Hope this helps.
Related
I have a Mapbox view which I'm setting up then adding as a subview. I thought it reasonable to expect that the child of that view would also be added when the Mapbox view is added. However, it seems that I need to call addSubView for each child of the first view.
Here's the entirety of my ViewController.swift:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var locationButton: UIButton!
#IBOutlet weak var buttonView: UIView!
#IBOutlet var mapView: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "mapbox://styles/billofbong/xxx/draft")
mapView = MGLMapView(frame: view.bounds, styleURL: url);
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.setCenter(CLLocationCoordinate2D(latitude: xxx, longitude: -xxx), zoomLevel: 15, animated: false)
mapView.showsUserLocation = true
mapView.showsHeading = true;
view.addSubview(mapView)
view.addSubview(buttonView)
}
#IBAction func onLocationButtonPressed(_ sender: Any) {
mapView.userTrackingMode = .followWithHeading
}
}
The reason I'm adding the Mapbox view as a subview instead of just having it be the original view in the ViewController is that changing the parameters didn't seem to do anything when it was the active view so I was stuck with the default map parameters.
Here's a screenshot of my storyboard:
Can anyone shed some light on the addSubView situation? What if I have a bunch of buttons? Do I really have to programmatically add each one? What am I doing wrong?
I deleted my ViewController, added a new one, linked all the IBOutlets, and now it works fine. I must have messed something up with the first ViewController.
Yes, you do have to add each button to your view. How should XCode figure out on itself where these elements should be located otherwise?
I wanted to code a map app for personal use (iOS) using swift 5. I have already been able to make it working by using a single file, but the code looks like a mess, so I decided to use multiple files and just call functions from the ViewController.
For a simple map view I already made it possible with this code:
//ViewController.swift
import UIKit
import Mapbox
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
mapInit()
// Do any additional setup after loading the view.
}
}
//mapInit.swift
import Foundation
import Mapbox
extension ViewController{
func mapInit(){
let mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Set the map’s center coordinate and zoom level.
mapView.setCenter(CLLocationCoordinate2D(latitude: 59.31, longitude: 18.06), zoomLevel: 9, animated: false)
view.addSubview(mapView)
}
}
but I tried to change the map style from the ViewController file using
mapView.styleURL = MGLStyle.darkStyleURL
but I got
Use of unresolved identifier mapView
I also tried to use
self.mapView.styleURL = MGLStyle.darkStyleURL
but now I got
Value of type 'ViewController' has no member 'mapView'
I also tried to add
var mapView: MGLMapView!
at the top of the ViewController
but now it crashes with the message
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Finally I tried to initialize the mapView from the ViewController.swift and change the style from the mapInit() but it changed nothing.
Does anyone know how to solve this problem?
If you add this
import Mapbox
import UIKit
class ViewController: UIViewController, MGLMapViewDelegate {
var mapView: MGLMapView!
override func viewDidLoad() {
super.viewDidLoad()
mapView = MGLMapView(frame: view.bounds)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.setCenter(CLLocationCoordinate2D(latitude: 0, longitude: 0), zoomLevel: 5, animated: false)
mapView.delegate = self
view.addSubview(mapView)
to your main ViewController, then you can access the mapview via an extension ViewController.
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/>
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 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.