2

我想以与 iOS6 相同的方式创建 MKUserTrackingBarButtonItem。就像一个浮动的 UIBarButtonItem。( http://cl.ly/image/1Q1K2S1A1H3N )

您能否就如何实现这一目标提出一些建议?

谢谢!

4

2 回答 2

9

最后,我按照@rckoenes 的建议手动创建了按钮。

方法如下(此处提供了一个工作项目: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];
于 2012-10-25T16:49:39.090 回答
1

如果您想将 jcalonso 的答案与自动布局一起使用,以便它支持纵向/横向 iPhone/iPad,只需删除在按钮上设置框架的行,并在调用 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]]; 
于 2013-09-30T17:21:00.250 回答