title: Swift: How to put a MapView in UIView (?)
body: <p>I'm pretty new in programming so I need a little bit of help. I write in Swift without using the Storyboard. I want to add a MKMapView(?) if you can say so? On a UIView. The problem is that I can't do that because it cannot convert the value/type to UIView. I have to write it in a UIViewController class. How I'm going to solve this? I don't know which codes I have to show you...</p>
  
  <p>But my app look like pretty much the same as the app in the video in the link. <a href="https://www.youtube.com/watch?v=77nQN0JzBH4" rel="nofollow">look att 5:08</a></p>
  
  <p>I want to have a Map inside of one of these cells.</p>
  
  <p>Hope someone can help me!:)</p>
  
  <p>Edit: I add the codes</p>
  
  <p>Here is the "Map" codes that I'm uing:</p>
  
  <pre><code>    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!)
  
  
  }
  
  }
  </code></pre>
  
  <p>In this block (if you can say so?) can I change the cell that I want. You can se that I've changed the color to red. I think it there I have to implement the Map but I can't.</p>
  
  <pre><code>import UIKit
  
  import MapKit
  
  
  class MapCell1: MapCell, MKMapViewDelegate {
  
  
  
  override func setupViews() {
  
  
          backgroundColor = UIColor.redColor()
  
  
  }
  
  
  }
  </code></pre>
  
  <p>And MapCell1 is a subclass of MapCell, and the Mapcell looks like this:</p>
  
  <pre><code>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)
  
  
  }
  
  }
  </code></pre>
  
  <p>And I don't know if these codes will help but I add them too.</p>
  
  <pre><code> 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() {
  
      }
  
  
  }
  }
  </code></pre>
  
  <p>And</p>
  
  <pre><code>    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) -&gt; Int {
      return 3
  }
  
  func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -&gt; 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) -&gt; CGSize {
      return CGSizeMake(frame.width / 3, frame.height)
  }
  
  func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -&gt; 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))
  
  }
  
  }
  </code></pre>