Swift:如何在UIView中放置MapView(?)

时间:2016-08-01 22:58:37

标签: ios iphone swift xcode uiview

我在编程方面很新,所以我需要一些帮助。我在没有使用故事板的情况下在Swift中编写。如果你能说的话,我想添加一个MKMapView(?)?在UIView上。问题是我无法做到这一点,因为它无法将值/类型转换为UIView。我必须在UIViewController类中编写它。我将如何解决这个问题?我不知道我要向你展示哪些代码......

但我的应用程序看起来与链接视频中的应用程序非常相似。 look att 5:08

我希望其中一个单元格中有一个Map。

希望有人可以帮助我!:)

编辑:我添加了代码

以下是我正在使用的“地图”代码:

    import UIKit
import MapKit

class ViewController: UIViewController {

var window: UIWindow?
var mapView: MKMapView?

override func viewDidLoad() {
    super.viewDidLoad()

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.view.backgroundColor = UIColor.whiteColor()

    self.mapView = MKMapView(frame: CGRectMake(0, 20, (self.window?.frame.width)!, 300))
    self.view.addSubview(self.mapView!)


}

}

在这个区块(如果可以这么说?)我可以更改我想要的单元格。你可以说我已经将颜色改为红色。我认为我必须实现Map,但我不能。

import UIKit

import MapKit


class MapCell1: MapCell, MKMapViewDelegate {



override func setupViews() {


        backgroundColor = UIColor.redColor()


}


}

MapCell1是MapCell的子类,Mapcell看起来像这样:

import UIKit
class MapCell: BaseCell, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
    cv.backgroundColor = UIColor.grayColor() //Here can I change the color in the whole UIcollectionView. So every three cells are gray right now.
    return cv
}()



let cellId = "cellId"

override func setupViews() {
    super.setupViews()


    addSubview(collectionView)
    addconstraintsWithFormat("H:|[v0]|", views: collectionView)
    addconstraintsWithFormat("V:|[v0]|", views: collectionView)


}

}

我不知道这些代码是否会有所帮助,但我也加了它们。

 import UIKit

class BaseCell: UICollectionViewCell {
override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
}

func setupViews() {

}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")

}

class BaseCell: UICollectionViewCell {


    func setupViews() {

    }


}
}

    import UIKit

class MenuBar: UIView, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.backgroundColor = UIColor.rgb(61, green: 91, blue: 151)
    cv.dataSource = self
    cv.delegate = self
    return cv
}()

let cellId = "cellId"
let imageNames = ["LocationMarker", "MapFeed", "ProfilePicture" ]

var viewController: ViewController?

override init(frame: CGRect) {
    super.init(frame: frame)

    collectionView.registerClass(MenuCell.self, forCellWithReuseIdentifier: cellId)

    addSubview(collectionView)
    addconstraintsWithFormat("H:|[v0]|", views: collectionView)
    addconstraintsWithFormat("V:|[v0]|", views: collectionView)

    let selectedIndexPath = NSIndexPath(forItem: 0, inSection: 0)
    collectionView.selectItemAtIndexPath(selectedIndexPath, animated: false, scrollPosition: .None)

    setupHorizontalBar()
}

var horizontalBarLefAnchorConstraint: NSLayoutConstraint?

func setupHorizontalBar() {
    let horizontalBarView = UIView()
    horizontalBarView.backgroundColor = UIColor.whiteColor()
    horizontalBarView.translatesAutoresizingMaskIntoConstraints = false
    addSubview(horizontalBarView)

    horizontalBarLefAnchorConstraint = horizontalBarView.leftAnchor.constraintEqualToAnchor(self.leftAnchor)
    horizontalBarLefAnchorConstraint?.active = true
    horizontalBarView.bottomAnchor.constraintEqualToAnchor(self.bottomAnchor).active = true
    horizontalBarView.widthAnchor.constraintEqualToAnchor(self.widthAnchor, multiplier: 1/4).active = true
    horizontalBarView.heightAnchor.constraintEqualToConstant(4).active = true
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

//        print(indexPath.item)
//        let x = CGFloat(indexPath.item) * frame.width / 2.65
//        horizontalBarLefAnchorConstraint?.constant = x
//        
//        UIView.animateWithDuration(0.75, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .CurveEaseOut, animations: {
//            self.layoutIfNeeded()
// 

       }, completion: nil)

    viewController?.scrollToMenuIndex(indexPath.item)


}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 3
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, forIndexPath: indexPath) as! MenuCell

    cell.imageView.image = UIImage(named: imageNames[indexPath.item])?.imageWithRenderingMode(.AlwaysTemplate)
    cell.tintColor = UIColor.rgb(39, green: 58, blue: 97)

    return cell
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    return CGSizeMake(frame.width / 3, frame.height)
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
    return 0
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

}


class MenuCell: BaseCell {

let imageView: UIImageView = {
    let iv = UIImageView()
    iv.image = UIImage(named: "LocationMarker")?.imageWithRenderingMode(.AlwaysTemplate)
    iv.tintColor = UIColor.rgb(39, green: 58, blue: 97)
    return iv
}()

override var highlighted: Bool {
    didSet {
        imageView.tintColor = highlighted ? UIColor.whiteColor() : UIColor.rgb(39, green: 58, blue: 97)
    }
}

override var selected: Bool {
    didSet {
        imageView.tintColor = selected ? UIColor.whiteColor() : UIColor.rgb(39, green: 58, blue: 97)
    }
}
override func setupViews() {
    super.setupViews()

    addSubview(imageView)
    addconstraintsWithFormat("H:[v0(28)]", views: imageView)
    addconstraintsWithFormat("V:[v0(28)]", views: imageView)

    addConstraint(NSLayoutConstraint(item: imageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
    addConstraint(NSLayoutConstraint(item: imageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))

}

}

2 个答案:

答案 0 :(得分:1)

使用MKMapView将自定义类设置为您的视图。 see below screenshot

答案 1 :(得分:0)

嗯...我认为这就是你想要的

import MapKit

func initMapView() {
    let mapView = MKMapView()
    mapView.delegate = self
    ...
    self.view.addSubView(mapView)
}