【问题标题】:Create a iOS6 Maps MKUserTrackingBarButtonItem button创建一个 iOS6 地图 MKUserTrackingBarButtonItem 按钮
【发布时间】:2012-10-25 09:57:56
【问题描述】:

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

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

谢谢!

【问题讨论】:

  • 只需在MKMapView 上方放一个UIButton
  • 这是我的第一种方法,但是,我需要做按钮的所有设计,对吗?我不能使用默认的 MKUserTrackingBarButtonItem ??
  • 不,MKUserTrackingBarButtonItemUIBarButtonItem
  • 您可以在视图底部添加一个透明工具栏,并在其中添加MKUserTrackingBarButtonItem,但我不确定它是否会像官方地图应用程序那样美观。
  • 我正在尝试手动创建它,我希望它看起来不错。感谢您的建议!

标签: iphone ios6 mkmapview uibarbuttonitem userlocation


【解决方案1】:

最后我按照@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];

【讨论】:

  • 这个解决方案的问题在于它不支持多个方向或设备(iPad 与 iPhone),因为您对按钮的框架进行了硬编码。我强烈建议改用 AutoLayout,这样您就不必担心这一点。如果我自己实现这个,我会在这里发布。感谢您的代码。
  • 查看此答案以使用 AutoLayout 添加用户位置按钮:stackoverflow.com/a/19100108/1103584
【解决方案2】:

如果您想将 jcalonso 的答案与 autolayout 一起使用,以便它支持纵向/横向 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]]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多