MapKit - problem with showing the user location and the MKPolygon Area - swift

I'm having a problem with trying to display the user location and the MKPolygon Area.
Here is the code about the MapView.swift:
import SwiftUI
import MapKit
let model = Model(filename: "ZONE_LIST")
var mapView = MKMapView() // (frame: UIScreen.main.bounds)
var theme = ""
struct MapView: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
let latDelta = model.overlayTopLeftCoordinate.latitude - model.overlayBottomRightCoordinate.latitude
let span = MKCoordinateSpan(latitudeDelta: fabs(latDelta), longitudeDelta: 0.99)
let region = MKCoordinateRegion(center: model.midCoordinate, span: span)
mapView.showsUserLocation = true
mapView.region = region
mapView.delegate = context.coordinator
return mapView
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func updateUIView(_ uiView: MKMapView, context: UIViewRepresentableContext<MapView>) {}
}
The Coordinator file instead is:
import MapKit
final class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
if theme == "Ciano" {
polygonView.strokeColor = .cyan
polygonView.fillColor = .cyan
} else if theme == "Rosso" {
polygonView.strokeColor = .init(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0)
polygonView.fillColor = .init(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0)
} else if theme == "Verde" {
polygonView.strokeColor = .init(red: 0/255, green: 255/255, blue: 0/255, alpha: 1.0)
polygonView.fillColor = .init(red: 0/255, green: 255/255, blue: 0/255, alpha: 1.0)
} else if theme == "Magenta" {
polygonView.strokeColor = .magenta
polygonView.fillColor = .magenta
} else if theme == "Giallo" {
polygonView.strokeColor = .yellow
polygonView.fillColor = .yellow
} else if theme == "Arancione" {
polygonView.strokeColor = .init(red: 255/255, green: 153/255, blue: 51/255, alpha: 1.0)
polygonView.fillColor = .init(red: 255/255, green: 153/255, blue: 51/255, alpha: 1.0)
} else if theme == "Verde Turchese" {
polygonView.strokeColor = .init(red: 50/255, green: 198/255, blue: 166/255, alpha: 1.0)
polygonView.fillColor = .init(red: 50/255, green: 198/255, blue: 166/255, alpha: 1.0)
} else if theme == "Blu" {
polygonView.strokeColor = .init(red: 0/255, green: 66/255, blue: 255/255, alpha: 1.0)
polygonView.fillColor = .init(red: 0/255, green: 66/255, blue: 255/255, alpha: 1.0)
} else {
polygonView.strokeColor = .init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0)
polygonView.fillColor = .init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0)
}
polygonView.alpha = 0.5
polygonView.lineWidth = 5.0
return polygonView
}
return MKOverlayRenderer()
}
}
To be honest, this is the code in the exact moment I have got the error trying to put the UserLocation on the Project, however the problem is this. I hope someone can help me.

Ok boyz, the problem has been resolved by myself, lol.
I just putted in the Coordinator.swift file the code that print out the marked area.
final class Coordinator: NSObject, MKMapViewDelegate {
var parent: MapView
init(_ parent: MapView) {
self.parent = parent
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if overlay is ModelMapOverlay {
return ModelMapOverlayView(overlay: overlay, overlayImage: UIImage(imageLiteralResourceName: "overlay_model"))
} else if overlay is MKPolyline {
let lineView = MKPolylineRenderer(overlay: overlay)
lineView.strokeColor = .green
return lineView
} else if overlay is MKPolygon {
let polygonView = MKPolygonRenderer(overlay: overlay)
if theme == "Ciano" {
polygonView.strokeColor = .cyan
polygonView.fillColor = .cyan
} else if theme == "Rosso" {
polygonView.strokeColor = .init(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0)
polygonView.fillColor = .init(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0)
} else if theme == "Verde" {
polygonView.strokeColor = .init(red: 0/255, green: 255/255, blue: 0/255, alpha: 1.0)
polygonView.fillColor = .init(red: 0/255, green: 255/255, blue: 0/255, alpha: 1.0)
} else if theme == "Magenta" {
polygonView.strokeColor = .magenta
polygonView.fillColor = .magenta
} else if theme == "Giallo" {
polygonView.strokeColor = .yellow
polygonView.fillColor = .yellow
} else if theme == "Arancione" {
polygonView.strokeColor = .init(red: 255/255, green: 153/255, blue: 51/255, alpha: 1.0)
polygonView.fillColor = .init(red: 255/255, green: 153/255, blue: 51/255, alpha: 1.0)
} else if theme == "Verde Turchese" {
polygonView.strokeColor = .init(red: 50/255, green: 198/255, blue: 166/255, alpha: 1.0)
polygonView.fillColor = .init(red: 50/255, green: 198/255, blue: 166/255, alpha: 1.0)
} else if theme == "Blu" {
polygonView.strokeColor = .init(red: 0/255, green: 66/255, blue: 255/255, alpha: 1.0)
polygonView.fillColor = .init(red: 0/255, green: 66/255, blue: 255/255, alpha: 1.0)
} else {
polygonView.strokeColor = .init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0)
polygonView.fillColor = .init(red: 0/255, green: 0/255, blue: 0/255, alpha: 1.0)
}
polygonView.alpha = 0.5
polygonView.lineWidth = 5.0
return polygonView
} else if let character = overlay as? Character {
let circleView = MKCircleRenderer(overlay: character)
circleView.strokeColor = character.color
return circleView
}
return MKOverlayRenderer()
}
}
Hope if someone will got the same problem could see this snippet to resolve it

Related

when i run my app nothing is showing(text fields,buttons...) except the Gradient background, I'm working on the interface for now

import UIKit
class MainVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func backgroundcolor(){
let newLayer = CAGradientLayer()
newLayer.colors = [UIColor.init(red: 0.2705882353, green: 0.4823529412, blue: 0.6156862745, alpha: 1).cgColor,UIColor.init(red: 0.6588235294, green: 0.8549019608, blue: 0.862745098, alpha: 1).cgColor]
newLayer.frame = view.frame
view.layer.addSublayer(newLayer)
}
}
In your viewDidLayoutSubviews, call your function. Any changes related to layout should be done in viewDidLayoutSubviews
import UIKit
class MainVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
backgroundColor()
}
func backgroundcolor(){
let newLayer = CAGradientLayer()
newLayer.colors = [UIColor.init(red: 0.2705882353, green: 0.4823529412, blue: 0.6156862745, alpha: 1).cgColor,UIColor.init(red: 0.6588235294, green: 0.8549019608, blue: 0.862745098, alpha: 1).cgColor]
newLayer.frame = view.frame
self.view.layer.insertSublayer(newLayer, at: 0)
}
}
Try this
let gradientView = UIView()
gradientView.frame = view.bounds
let newLayer = CAGradientLayer()
newLayer.colors = [UIColor.init(red: 0.2705882353, green: 0.4823529412, blue: 0.6156862745, alpha: 1).cgColor,UIColor.init(red: 0.6588235294, green: 0.8549019608, blue: 0.862745098, alpha: 1).cgColor]
newLayer.frame = gradientView.bounds
gradientView.layer.addSublayer(newLayer)
self.view.addSubview(gradientView)
self.view.sendSubviewToBack(gradientView)

How to customize outlined button using MDCButtons in Swift?

I'm customizing my UIButton using MDCButton. I want to make my button outlined and customize its color.
In this case, I'm using MDCOutlinedButtonThemer.
I also implement MDCButton (MDCButtonColorThemer) with custom color in other button and it working.
I've tried to set my button with default MDCOutlinedButton and it works.
This is my code :
MDCOutlinedButtonThemer.applyScheme(buttonScheme, to: self.btnAddToCart)
MDCButtonColorThemer.applySemanticColorScheme(ApplicationScheme.shared.colorScheme, to: self.btnBuy)
This is the ApplicationScheme.swift :
public let colorScheme: MDCColorScheming = {
let scheme = MDCSemanticColorScheme(defaults: .material201804)
//TODO: Customize our app Colors after this line
scheme.primaryColor = UIColor(red: 255.0 / 255.0, green: 201.0 / 255.0, blue: 46.0 / 255.0, alpha: 1)
//scheme.primaryColorVariant = UIColor(red: 68.0/255.0, green: 44.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.onPrimaryColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
scheme.secondaryColor = UIColor(red: 254.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.onSecondaryColor = UIColor(red: 68.0/255.0, green: 44.0/255.0, blue: 46.0/255.0, alpha: 1.0)
scheme.surfaceColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.onSurfaceColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
scheme.backgroundColor = UIColor(red: 255.0/255.0, green: 201.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.onBackgroundColor = UIColor(red: 68.0/255.0, green: 44.0/255.0, blue: 46.0/255.0, alpha: 1.0)
//scheme.errorColor = UIColor(red: 197.0/255.0, green: 3.0/255.0, blue: 43.0/255.0, alpha: 1.0)
return scheme
}()
I want to make "Add to cart" button's border color same with "buy" button's color
You can use next color settings for creation MDCButton:
let myButton = MDCButton()
myButton.setTitle("Add to cart", for: .normal)
myButton.isUppercaseTitle = false
myButton.addTarget(self, action: #selector(myButtonAction(_:)), for: .touchUpInside)
myButton.frame = CGRect(x: 10, y: 10, width: 150, height: 40)
myButton.setBackgroundColor(UIColor.white)
myButton.setTitleColor(UIColor.blue, for: UIControl.State.normal)
myButton.setBorderColor(UIColor.lightGray, for: UIControl.State.normal)
myButton.setBorderWidth(1.0, for: UIControl.State.normal)
myButton.layer.cornerRadius = 5
view.addSubview(myButton)

Describing or using String to Explain a UIColor

Trying to simplify my implementation of coloring of each cell row object.
This is how I currently add a color to the object in each row:
progressViewLeft.primaryColor = Colors.Stage1
"Colors" is a struct set up like so:
struct Colors {
static let Stage1 = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
static let Stage2 = UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
}
I'm looking for a way to instead of writing: "Colors.Stage1" and on the next one: "Colors.Stage2" something like this:
progress.primaryColor = String("Colors.Stage" + String(indexpath.row))
You can try
let allColors = [ UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0) ,
UIColor(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)]
Then
progress.primaryColor = allColors[indexpath.row]

Navigation Controller. Background color

For one controller I have settings:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
and
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
var offset = scrollView.contentOffset.y / 150
if offset > 1 {
offset = 1
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
self.navigationItem.title = name
} else {
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 82/255, green: 76/255, blue: 70/255, alpha: offset)
self.navigationItem.title = ""
}
}
The main problem is that when i press back button, settings save. In the end i have white NavigationController. How can I make the settings not taken from the last controller?
func makeSearchController() {
searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.tintColor = .white
searchController.searchBar.placeholder = "Блюдо или продукт ..."
}
You can reset the navigation controller's color in viewWillDisappear, like this:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 221/255, green: 221/255, blue: 225/255, alpha: offset) //gray color
UIApplication.shared.statusBarView?.backgroundColor = UIColor(red: 221/255, green: 221/255, blue: 225/255, alpha: offset) //gray color
}

Can i use 'for' loop on this?

I'm wondering if i may use 'for' loop for this code. Please forgive me, I know that is kind of a lame question, but I'm new to swift. Hope that you can help me here, guys!
Thanks a lot everyone!
Code:
override func viewDidLoad() {
super.viewDidLoad()
// Background color
let kolorTla = UIColor(red: 0/255.0, green: 66/255.0, blue: 132/255.0, alpha: 1.0)
view.backgroundColor = kolorTla
// Icons border
ramka.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
ramka.layer.cornerRadius = 5.0
ramka.layer.borderWidth = 3
// Image
skill1.image = UIImage(named: "english")
// Image border
skill1.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
skill1.layer.cornerRadius = 5.0
skill1.layer.borderWidth = 3
skill1.contentMode = .scaleAspectFit
// Image
skill2.image = UIImage(named: "literature")
// Image border
skill2.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
skill2.layer.cornerRadius = 5.0
skill2.layer.borderWidth = 3
skill2.contentMode = .scaleAspectFit
// Image
skill3.image = UIImage(named: "idea1")
// Image border
skill3.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
skill3.layer.cornerRadius = 5.0
skill3.layer.borderWidth = 3
skill3.contentMode = .scaleAspectFit
You can to some extent. You would just need to define an array of your items and loop over them. Not sure if it saves you much code wise but does make it a little more understandable.
override func viewDidLoad() {
super.viewDidLoad()
// Background color
let kolorTla = UIColor(red: 0/255.0, green: 66/255.0, blue: 132/255.0, alpha: 1.0)
view.backgroundColor = kolorTla
// Icons border
ramka.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
ramka.layer.cornerRadius = 5.0
ramka.layer.borderWidth = 3
// Set Image
skill1.image = UIImage(named: "english")
skill2.image = UIImage(named: "literature")
skill3.image = UIImage(named: "idea1")
// Set Image border
for skill in [skill1, skill2, skill3] {
skill.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
skill.layer.cornerRadius = 5.0
skill.layer.borderWidth = 3
skill.contentMode = .scaleAspectFit
}
}
Better approach at least in my opinion would be to create a simple function to handle this instead.
override func viewDidLoad() {
super.viewDidLoad()
// Background color
let kolorTla = UIColor(red: 0/255.0, green: 66/255.0, blue: 132/255.0, alpha: 1.0)
view.backgroundColor = kolorTla
// Icons border
ramka.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
ramka.layer.cornerRadius = 5.0
ramka.layer.borderWidth = 3
// Set Images
setupImageView(imageView: skill1, imageName: "english")
setupImageView(imageView: skill2, imageName: "literature")
setupImageView(imageView: skill3, imageName: "idea1")
}
func setupImageView(imageView: UIImageView, imageName: String) {
// Set Image
imageView.image = UIImage(named: imageName)
// Set Image border
imageView.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
imageView.layer.cornerRadius = 5.0
imageView.layer.borderWidth = 3
imageView.contentMode = .scaleAspectFit
}
While you're not able to use a for loop to modify the variable names and loop through them, you could put your skill instances in an array and loop through them that way.
...
skill1.image = UIImage(named: "english")
skill2.image = UIImage(named: "literature")
skill3.image = UIImage(named: "idea1")
let skills = [skill1, skill2, skill3]
for skill in skills {
skill.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
skill.layer.cornerRadius = 5.0
skill.layer.borderWidth = 3
skill.contentMode = .scaleAspectFit
}
Yes, you can. Place skill1,skill2, and skill3 in an array and iterate over it like this:
var objectArray = [skill1,skill2,skill3]
for object in objectArray
{
object.layer = ....
}
You could just extend your ImageView
extension UIImageView {
func addCustomLayer() { // add arguments to function if you wish to change the value assigned
self.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
self.layer.cornerRadius = 5.0
self.layer.borderWidth = 3
self.contentMode = .scaleAspectFit
}
}
}
Then you call that method on each UIImageView
for each in [skill1, skill2, skill3] {
each.addCustomLayer()
}