【问题标题】:Trying to change MKPointAnnotation colour but losing the title尝试更改 MKPointAnnotation 颜色但丢失标题
【发布时间】:2015-06-01 15:51:57
【问题描述】:

我正在尝试将我的图钉颜色更改为紫色,但当我这样做时,我会失去标题。代码是:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationController.navigationBarHidden=YES;

    //init the location manager
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager requestWhenInUseAuthorization];
    self.mapView.showsUserLocation=YES;

    self.userGeoPoint=self.message[@"userLocation"];
    self.pinView = [[MKPointAnnotation alloc] init];

    self.pinView.title=self.message[@"fromUser"];
    self.coord = CLLocationCoordinate2DMake(self.userGeoPoint.latitude, self.userGeoPoint.longitude);
    self.pinView.coordinate=self.coord;
    //use a slight delay for more dramtic zooming
    [self performSelector:@selector(addPin) withObject:nil afterDelay:0.5];
}

-(void)addPin{

    [self.mapView addAnnotation:self.pinView];
    [self.mapView selectAnnotation:self.pinView animated:YES];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

}

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotationPoint
{
    if ([annotationPoint isKindOfClass:[MKUserLocation class]])//keep the user as default
        return nil;

    static NSString *annotationIdentifier = @"annotationIdentifier";
    MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotationPoint reuseIdentifier:annotationIdentifier];
    pinView.pinColor = MKPinAnnotationColorPurple;
    //now we can throw an image in there

    return pinView;
}

我尝试为 MKPinAnnotation 设置标题属性,但没有。无论如何我可以解决这个问题吗?

【问题讨论】:

    标签: ios objective-c mkpinannotationview


    【解决方案1】:

    viewForAnnotation中,需要将canShowCallout设置为YES(默认为NO):

    pinView.pinColor = MKPinAnnotationColorPurple;
    pinView.canShowCallout = YES;
    



    几个不相关的点:
    1. 看起来您有一个名为pinView 的属性,类型为MKPointAnnotation,您正在使用该属性在viewDidLoad 中创建注释对象。然后在viewForAnnotation 中,您有一个也名为pinView 类型为MKPinAnnotationView 的局部变量。虽然这里没有命名冲突,但它会导致并暗示一些概念上的混乱。 MKPointAnnotation 是一个注解 model 类,而MKPinAnnotationView 是一个注解 view 类——它们是完全不同的东西。例如,最好将注释属性命名为 pinuserAnnotation
    2. 这条评论在viewForAnnotation:

      //now we can throw an image in there
      

      似乎暗示您此时可以在视图中设置自定义图像。不建议在MKPinAnnotationView 中设置自定义图像,因为该类旨在以三种颜色之一显示默认图钉图像。要使用自定义图像,请创建一个普通的 MKAnnotationView

    【讨论】:

    • 谢谢!现在工作。也感谢您的指点。我会修改代码。
    【解决方案2】:

    pinColor 属性在 iOS 9 中已弃用。从 iOS 9 开始,您应该使用 pinTintColor,它采用 UIColor 而不是 MKPinAnnotationColor

    旧: MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil]; view.pinColor = MKPinAnnotationColorPurple;

    新: MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil]; view.pinTintColor = [UI Color purpleColor];

    Apple docs 中提供了更多信息。

    【讨论】:

      【解决方案3】:

      我刚升级到ios 10,苹果改了api。

      pinColor 不再使用,改为使用 tintColor

      新:

       MKPinAnnotationView *view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
       view.tintColor = [UIColor purpleColor];
      

      【讨论】:

        【解决方案4】:

        请注意,当用户点击时会显示图钉标题:

        如果此属性的值为 true,则当用户点击选定的注释视图时会显示一个标准标注气泡。标注使用关联注释对象中的标题和副标题文本。 (https://developer.apple.com/documentation/mapkit/mkannotationview/1452451-canshowcallout)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-24
          • 1970-01-01
          • 2010-11-02
          • 2012-01-14
          • 2023-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多