2017-10-13 9 views
-2

J'ai un code que j'ai trouvé en ligne qui correspond parfaitement à mon projet. Le problème est que je n'utilise pas de story-boards du tout. Au lieu d'utiliser un fichier de storyboard pour créer le UIView (CustomCallOutView), je viens de créer une classe avec une sous-classe de UIView. Voici le code qui nécessite un fichier nib mais je ne veux pas les utiliser. Comment puis-je y parvenir? Le code est ci-dessous. Je vous remercie!Swift 4: Quel est l'équivalent de charger un fichier nib par programme, sans story-boards?

func mapView(_ mapView: MKMapView, 
     didSelect view: MKAnnotationView) 
{ 
    // 1 
    if view.annotation is MKUserLocation 
    { 
     // Don't proceed with custom callout 
     return 
    } 
    // 2 
    let starbucksAnnotation = view.annotation as! StarbucksAnnotation 
    let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil) 
    let calloutView = views?[0] as! CustomCalloutView 
    calloutView.starbucksName.text = starbucksAnnotation.name 
    calloutView.starbucksAddress.text = starbucksAnnotation.address 
    calloutView.starbucksPhone.text = starbucksAnnotation.phone 
    calloutView.starbucksImage.image = starbucksAnnotation.image 
    let button = UIButton(frame: calloutView.starbucksPhone.frame) 
    button.addTarget(self, action: #selector(ViewController.callPhoneNumber(sender:)), for: .touchUpInside) 
    calloutView.addSubview(button) 
    // 3 
    calloutView.center = CGPoint(x: view.bounds.size.width/2, y: -calloutView.bounds.size.height*0.52) 
    view.addSubview(calloutView) 
    mapView.setCenter((view.annotation?.coordinate)!, animated: true) 
} 

Répondre

0

En supposant que CustomCalloutView ne fait rien sophistiqué:

let calloutView = CustomCalloutView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) 
0

Si vous voulez créer un UIView sous-classe qui est charge une pointe vous pouvez utiliser les éléments suivants:

class YourViewWithNib: UIView { 
    required init?(coder: NSCoder) { 
     super.init(coder: coder) 
     loadNib() 
    } 


    override init(frame: CGRect) { 
     super.init(frame: frame) 
     bounds = frame 
     loadNib() 
    } 

    func loadNib() { 
     let nib = Bundle.main.loadNibNamed(
      "YourNibFileName", 
      owner: self, 
      options: nil 
     ) 
     let view = nib![0] as! UIView 

     view.translatesAutoresizingMaskIntoConstraints = false 
     addSubview(view) 

     // if you are using autolayout 
     let views = ["nibView": view] 
     let hconstraints = NSLayoutConstraint.constraints(
      withVisualFormat: "H:|[nibView]|", 
      metrics: nil, 
      views: views 
     ) 
     NSLayoutConstraint.activate(hconstraints) 

     let vconstraints = NSLayoutConstraint.constraints(
      withVisualFormat: "V:|[nibView]|", 
      metrics: nil, 
      views: views 
     ) 
     NSLayoutConstraint.activate(vconstraints) 
    } 
}  

Pour ajouter votre vue personnalisée, vous jus Vous pouvez utiliser YourViewWithNib(frame: aFrame) ou ajouter une vue YourViewWithNib dans n'importe quel XIB ou Storyboard.