【问题标题】:UIImageView subview of a MapViewMapView 的 UIImageView 子视图
【发布时间】:2012-11-07 10:54:10
【问题描述】:

我必须添加一个 ImageView 作为 MapView 的子视图。我不能通过代码来完成,因为 ImageView 总是落后于 mapView。我不能在 IB 中做到这一点,因为在文件的所有者中 UIImageView 始终是 mapView 的外部尊重(我尝试了几次)。我使用的是 Xcode 4.5。我可以添加 UIImageView 作为 MapView 的子视图吗?

这是 MapViewController.h 中的代码

 @property (nonatomic, strong) UIImageView *iphoneLegenda;
 @property (nonatomic, strong)IBOutlet MKMapView *mapView;

在 MapViewController.m 中

@synthesize iphoneLegenda;
@synthesize mapView;

- (void)viewDidLoad
{
  //.....
    self.iphoneLegenda.frame = CGRectMake(self.mapView.bounds.origin.x, 
                            self.mapView.bounds.origin.y, 200, 100);

  //......

   [self.mapView addSubview:self.iphoneLegenda];
   [self.iphoneLegenda bringSubviewToFront:self.mapView];
  //.....
}

【问题讨论】:

  • 为什么要在地图视图中使用 UIImageView?这段代码错了[self.iphoneLegenda bringSubviewToFront:self.mapView];,应该是[self.mapView bringSubviewToFront:self.iphoneLegenda];

标签: ios uiimageview mkmapview addsubview


【解决方案1】:

我认为 AntonPalich 和 MashDup 的答案是最好的方法。 要在 mapView 中添加子视图,您必须添加 QuartzCore.framework,代码如下:

- (void)viewDidLoad
{
 CALayer *layer = [CALayer layer];
 layer.backgroundColor = [[UIColor whiteColor] CGColor];
 layer.bounds = CGRectMake(mapView.frame.origin.x, mapView.frame.origin.y, 200, 100);
 [[mapView layer] addSublayer:layer];
}

【讨论】:

    【解决方案2】:

    如果您希望图像视图位于地图视图的顶部,就像您使用的是 xib 一样。 mapview 已经在一个视图上,只需在 xib 中添加 imageview 就像你为 mapview 所做的一样并将其链接起来。使该 alpha 在 xib 中具有 0(以后更适合动画)。然后在您的代码中,我假设您想要一个按钮或某种按钮来打开它。 所以换行有iboutlet。

    @property (nonatomic, strong) IBOutlet UIImageView *iphoneLegenda;
    

    然后是淡入淡出的动作:

    [UIView animateWithDuration:0.5 animations:^{
            [iphoneLengenda setAlpha:1.];
        }];
    

    不必乱设置框架等。

    【讨论】:

      【解决方案3】:

      这个方案怎么样:

      UIView
       - MKMapView
       - UIImageView
      

      MKMapView 将与 UIView 大小相同,并且 UIImageView 将始终位于顶部。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        • 2018-03-03
        • 1970-01-01
        • 2019-09-08
        • 1970-01-01
        • 1970-01-01
        • 2015-04-22
        相关资源
        最近更新 更多