I am using an xib to display custom info window in google maps. The info window is not getting shown

61 Views Asked by At

My XIB Class With Self initialization

final class MarkerWindow: UIView {
    @IBOutlet var containerView: UIView!
    @IBOutlet weak var imgUser: UIImageView!
    @IBOutlet weak var lblUserName: UILabel!
    @IBOutlet weak var lblEmergency: UILabel!
    @IBOutlet weak var lblDistance: UILabel!
    @IBOutlet weak var vwEmergency: UIView!
    @IBOutlet weak var widthConst: NSLayoutConstraint!
    @IBOutlet weak var heightConst: NSLayoutConstraint!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initSubViews()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        self.initSubViews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        vwEmergency.CornerRadius = (vwEmergency.frame.height) / 2
    }

    private func initSubViews() {
    let nib = UINib(nibName: String(describing: type(of: self)), bundle: Bundle(for: type(of: self)))
        nib.instantiate(withOwner: self, options: nil)
        containerView.translatesAutoresizingMaskIntoConstraints = false
        widthConst.constant = 388
        heightConst.constant = 100
        addSubview(containerView)
        self.addConstraints()
    }
    
    private func addConstraints() {
        NSLayoutConstraint.activate([
            self.topAnchor.constraint(equalTo: containerView.topAnchor),
            self.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            self.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),
            self.bottomAnchor.constraint(equalTo: containerView.bottomAnchor)
        ])
    }
}

Delegate Method Called

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
    let customInfoWindow = MarkerWindow()
    return customInfoWindow
}
  • Outlets Connected in XIB Class Given Through File Owner

  • When Trying to initialize xib from View Controller by other methods Getting Error Bad Access Code - 2.

1

There are 1 best solutions below

0
Fiza Saiyed On BEST ANSWER

I called xib in viewDidLoad() instead of info window method.

var customView = MarkerWindow()
customView = MarkerWindow(frame: CGRect(x: 0, y: 0, width: 388, height: 100))

Then I return the topmost level UIView in the method as:

func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? { 
    return customView.containerView
}

And, I can see my xib view now. I also removed the MarkerWindow class from topmost view of xib. Now it is only in FileOwner.