ios - 从 UIViewController 获取代码到 UIView

标签 ios swift uiview uiviewcontroller mapbox

我想在 UIView 中创建这张 map 。这可能吗?如果可以,您能告诉我如何将此代码从 UIViewController 带到 UIView 吗?我正在尝试将 MapBox map 放入 UIView 中,但只有关于如何将其导入 UIViewController 的说明

这段代码看起来像一个 UIView 类:

import Mapbox

class mapboxMap: UIViewController, MGLMapViewDelegate {

var mapView: MGLMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    mapView = MGLMapView(frame: view.bounds)
    mapView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]

    // set the map's center coordinate
    mapView.setCenterCoordinate(CLLocationCoordinate2D(latitude: 40.7326808,
        longitude: -73.9843407),
        zoomLevel: 10, animated: false)
    view.addSubview(mapView)

    // Set the delegate property of our map view to self after instantiating it.
    mapView.delegate = self

    // Declare the marker `hello` and set its coordinates, title, and subtitle
    let hello = MGLPointAnnotation()
    hello.coordinate = CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407)
    hello.title = "Hello world!"
    hello.subtitle = "Welcome to my marker"
    mapView.addAnnotation(hello)

    let hello2 = MGLPointAnnotation()
    hello2.coordinate = CLLocationCoordinate2D(latitude: 40.7526808, longitude: -73.9843407)
    hello2.title = "Hello world!"
    hello2.subtitle = "Welcome to my marker"
    mapView.addAnnotation(hello2)


}

// Use the default marker; see our custom marker example for more information
func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
    return nil
}

func mapView(mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
    return true
}

override func viewDidAppear(animated: Bool) {
    // Wait a bit before setting a new camera.

    // Create a camera that rotates around the same center point, back to 0°.
    // `fromDistance:` is meters above mean sea level that an eye would have to be in order to see what the map view is showing.
    let camera = MGLMapCamera(lookingAtCenterCoordinate: mapView.centerCoordinate, fromDistance: 9000, pitch: 45, heading: 0)

    // Animate the camera movement over 5 seconds.
    mapView.setCamera(camera, withDuration: 2.5, animationTimingFunction: CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut))
}

最佳答案

我认为您要做的是“在 subview 中添加 UIViewController”。尝试使用 Container view Controller .以下线程可能对您有所帮助。

add UIViewController in subview

关于ios - 从 UIViewController 获取代码到 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35666094/

相关文章:

swift - 自定义 MKAnnotation 字典输入 (CLLocationCooperative2d)

iphone - 在 Objective C 中调用 curl 命令

iphone - 下载未完全完成时限制用户播放文件

xcode - 在应用程序购买 ios 中找不到产品

ios - 使用 AVPlayer 在 UIView 中播放延时

ios - 以编程方式根据 subview (标签)创建具有大小的 UIView

android - 如何在检索 ListView 的项目时显示 "Loading..."文本

ios - 相同的 CCMenu 添加到多个场景

iphone - 合并 UITableViewCells

ios - TextLabel 在 ScrollView 中被截断