Would anyone be able to show me how to add a MKUserTrackingBarButtonItem to my toolbar in Interface Builder? I have a UIBarButtonItem on my UIToolbar whose class I have set to MKUserTrackingBarButtonItem, but this doesn't seem the right way to do it.
I have the following property:
@property (nonatomic, strong) IBOutlet MKUserTrackingBarButtonItem *trackingButton;
And I can add the button in code by using:
trackingButton = [[MKUserTrackingBarButtonItem alloc] initWithMapView:mapView];
NSMutableArray *items = [[NSMutableArray alloc] initWithArray:toolbar.items];
[items insertObject:trackingButton atIndex:0];
[toolbar setItems:items];
But I'm just missing how to do it in IB.
Unfortunately this doesn't seem possible in IB due to the designated initializer of MKUserTrackingBarButtonItem. You have to instantiate it and add it to the toolbar programmatically, as you are doing.
You can just add a UIBarButtonItem, and then make it a MKUserTrackingBarButtonItem in the class field in the identity inspector/IB sidebar, and add the button as an IBOutlet and then for it to appear you have to set the mapView property programmatically. With swift this can be done nicely in didSet:
@IBOutlet weak var trackingButton: MKUserTrackingBarButtonItem! {
didSet {
trackingButton.mapView = self.mapView;
}
}
Or you can subclass and make mapView an IBOutlet so you can connect it in IB:
class UserTrackingBarButtonItem : MKUserTrackingBarButtonItem {
@IBOutlet override var mapView : MKMapView? {
get {
return super.mapView;
}
set {
super.mapView = newValue;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With