Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How would I go about adding a UIButton as an overlay to a mapView? This is what I have so far but it doesn't show up:

This is what I have:

UIButton *toMenu = [UIButton buttonWithType:UIButtonTypeRoundedRect]; toMenu.frame = CGRectMake(139, 539, 60, 60); [toMenu addTarget:self action:@selector(goToMenu:) forControlEvents:UIControlEventTouchUpOutside]; [toMenu setTitle:@"Arrow" forState:UIControlStateNormal]; [self.mapView addSubview:toMenu]; toMenu.hidden = false;

I also added the [self.mapView bringSubviewToFront:toMenu]; but when running the app nothing shows up :(..

Try adding the button to your view instead of your map, so instead of self.mapView addSubview try [self.view addSubview:toMenu]; or[self.view insertSubview:toMenu above: self.mapView];

Right so that worked.... Why? shouldn't the map be able to add subviews? Something I haven't tested is that perhaps the map did add it but the frame was created out of the view. I will move the parameters and see...

EDIT:

Checked but same issue, button doesn't show up! By using self.view it says that the 'view' is the view with the tap gesture... so I guess that would be the map in any case. Maybe theres a conflict there. I am quite new to this so still discovering stuff..

As developers you'll forever be learning. I had the same issue as you, but I wanted to add a table view to the map. I could be wrong but my guess is that the map view can only have one subview, the legal button.

1 Answer

Try adding the button to your view instead of your map, so instead of self.mapView addSubview try [self.view addSubview:toMenu]; or[self.view insertSubview:toMenu above: self.mapView];