I am trying to put a button in the corner of my MKMapView to control whether the map stays locked on to the user's location. What I have in mind is to create a UIView
with a button on and add it over my MKMapView
(not as an annotation or something) I can't fig开发者_StackOverflow社区ure this out with Interface Builder.
How can I add this button programmatically?
Controlling whether it actually follows the user etc. is already sorted - just need the button for it.
It looks like you are directly setting the controller's view
outlet to an MKMapView
object rather than a UIView
object containing the MKMapView
object. You cannot drop the button in such case on top of the MKMapView
object in the IB
. There are two ways you can deal with this,
- Declare an outlet for the button and drop the button in the
IB
. This needn't be on top of theMKMapView
object. Set the outlet to, say, abutton
property. Then inviewDidLoad
do[self.view addSubview:self.button];
after setting thebutton
'sframe
. (or) - Drop a new
UIView
object in IB and put theMKMapView
object inside it. Set the controller'sview
to this containerUIView
object. Later drop the button on top of theMKMapView
object and set it to its appropriate location.
精彩评论