GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - nevinchanyi/Wrapper: Wrap a custom UIView or UIViewController for SwiftUI

BranchesTags
Open more actions menu

Latest commit

 

History

25 Commits
25 Commits

Folders and files

NameName
Last commit message
Last commit date
.swiftpm/xcode/package.xcworkspace/xcshareddata
.swiftpm/xcode/package.xcworkspace/xcshareddata
 
 
Sources
Sources
 
 
Tests/WrapperTests
Tests/WrapperTests
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
Package.swift
Package.swift
 
 
README.md
README.md
 
 

Repository files navigation

Wrapper

Wrap a custom UIView or UIViewController in SwiftUI.

Installation

Swift Package Manager

https://github.com/nevinchanyi/Wrapper.git

Usage

View.

import SwiftUI

// 1. import the package
import Wrapper

struct MapView: View {
    
    // 2. Define a view-model, which contains an instance of UIView and 
    @StateObject var viewModel = MapViewModel()
    
    var body: some View {
    
    // 3. Pass the instance to the `WrapperView`
        WrapperView(view: viewModel.mapView)
        
        // 4. Set some custom view attributes
            .frame(width: 100, height: 100)
    }
}

View-Model with delegate.

import MapKit

// 5. Conform to `NSObject` in case you want to use delegation methods.
class ContentViewModel: NSObject, ObservableObject {
    
    // 6. Created instance for Wrapper
    let mapView = MKMapView()
    
    override init() {
        super.init()
        // 7. Can use delegation here, but pay attention, you have to `override init`
        mapView.delegate = self
        setCenter()
        addAnnotation()
    }
    
    func setCenter() {
        var region = MKCoordinateRegion()
        region.center = CLLocationCoordinate2D(latitude: 40.780735, longitude: -73.966350)
        region.span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
        mapView.setRegion(region, animated: true)
    }
    
    func addAnnotation() {
        let annotation = MKPointAnnotation()
        annotation.title = "Central Park"
        annotation.coordinate = CLLocationCoordinate2D(latitude: 40.780735, longitude: -73.966350)
        mapView.addAnnotation(annotation)
    }
}

// 8. ViewModel conforms protocol MKMapViewDelegate
extension ContentViewModel: MKMapViewDelegate {
    
    func mapView(_ mapView: MKMapView, didSelect annotation: MKAnnotation) {
        print(annotation.title, annotation.coordinate)
    }
}

Available examples

MKMapView, WKWebView, UITextView, UICalendarView

About

Wrap a custom UIView or UIViewController for SwiftUI

Topics

swiftswiftuiuiviewuiviewcontrolleruiviewcontrollerrepresentableuiviewrepresentablewrapper

Resources

Readme
MIT license
Activity

Stars

4 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.