jcalonso
jcalonso

Reputation: 1503

Create a iOS6 Maps MKUserTrackingBarButtonItem button

I want to create a MKUserTrackingBarButtonItem the same way iOS6 have it. Like a floating UIBarButtonItem. ( http://cl.ly/image/1Q1K2S1A1H3N )

Can you give some advice in how to achieve this?

Thx!

Upvotes: 2

Views: 2803

Answers (2)

DiscDev
DiscDev

Reputation: 39052

If you want to use jcalonso's answer with autolayout so that it supports portrait/landscape iPhone/iPad, simply remove the line that sets the frame on the button, and add these constraints after calling addSubView:

[mapView setTranslatesAutoresizingMaskIntoConstraints:NO];
[userHeadingBtn setTranslatesAutoresizingMaskIntoConstraints:NO];

NSDictionary *dict = NSDictionaryOfVariableBindings(mapView, userHeadingBtn);

//Map has user heading btn
[mapView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=175)-[userHeadingBtn(30)]-|" options:0 metrics:0 views:dict]];
[mapView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=175)-[userHeadingBtn(39)]-|" options:0 metrics:0 views:dict]];

//Continue with adding the map to the baseview
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mapView]|" options:0 metrics:0 views:dict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[mapView]|" options:0 metrics:0 views:dict]]; 

Upvotes: 1

jcalonso
jcalonso

Reputation: 1503

Finally I followed the recomendation of @rckoenes and created the button manually.

Here is how (A working project is available here: https://github.com/jcalonso/iOS6MapsUserHeadingButton ):

//User Heading Button states images
UIImage *buttonImage = [UIImage imageNamed:@"greyButtonHighlight.png"];
UIImage *buttonImageHighlight = [UIImage imageNamed:@"greyButton.png"];
UIImage *buttonArrow = [UIImage imageNamed:@"LocationGrey.png"];

//Configure the button
userHeadingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[userHeadingBtn addTarget:self action:@selector(startShowingUserHeading:) forControlEvents:UIControlEventTouchUpInside];
//Add state images
[userHeadingBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];
[userHeadingBtn setBackgroundImage:buttonImage forState:UIControlStateNormal];
[userHeadingBtn setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[userHeadingBtn setImage:buttonArrow forState:UIControlStateNormal];

//Button shadow
userHeadingBtn.frame = CGRectMake(5,425,39,30);
userHeadingBtn.layer.cornerRadius = 8.0f;
userHeadingBtn.layer.masksToBounds = NO;
userHeadingBtn.layer.shadowColor = [UIColor blackColor].CGColor;
userHeadingBtn.layer.shadowOpacity = 0.8;
userHeadingBtn.layer.shadowRadius = 1;
userHeadingBtn.layer.shadowOffset = CGSizeMake(0, 1.0f);

[self.mapView addSubview:userHeadingBtn];

Upvotes: 9

Related Questions