2

I customized my MKAnnotationView, when I touch it, it enter following code

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    CellThumbnailView *thumbnailView = (CellThumbnailView *)view;

    [mapView setCenterCoordinate:thumbnailView.thumbnail.coordinate animated:YES];

    CGSize size = [thumbnailView.thumbnail getThumbnailSize];

    thumbnailView.frame = CGRectMake(0, 0, size.width, size.height);
    thumbnailView.centerOffset = CGPointMake(0, -[thumbnailView.thumbnail getThumbnailCenterOffset] + size.height / 2);

    // Center map at annotation point
    thumbnailView.imageView.center = CGPointMake(size.width / 2, size.height/ 2);
    thumbnailView.imageView.bounds = CGRectMake(0, 0, size.width, size.height);
    thumbnailView.imageView.image = [UIImage imageNamed:[thumbnailView.thumbnail getPopupImageName]];

    CGSize labelOffset = [thumbnailView.thumbnail getThumbnailLabelOffset];
    thumbnailView.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 45, 20)];
    thumbnailView.titleLabel.backgroundColor = [UIColor clearColor];
    thumbnailView.titleLabel.textColor = [UIColor blueColor];
    thumbnailView.titleLabel.shadowColor = [UIColor colorWithWhite:1 alpha:0.5];
    thumbnailView.titleLabel.shadowOffset = CGSizeMake(0, -1);
    thumbnailView.titleLabel.font = [UIFont boldSystemFontOfSize:17];
    thumbnailView.titleLabel.alpha = 1;
    thumbnailView.titleLabel.minimumScaleFactor = .8f;
    thumbnailView.titleLabel.adjustsFontSizeToFitWidth = YES;
    thumbnailView.titleLabel.center = CGPointMake(thumbnailView.center.x - labelOffset.width, thumbnailView.center.y - labelOffset.height);
    thumbnailView.titleLabel.text = [NSString stringWithFormat:@"%dG", thumbnailView.thumbnail.traffic];
    [thumbnailView addSubview:thumbnailView.titleLabel];

    self.location.text = thumbnailView.thumbnail.desc;
    self.traffic.text = thumbnailView.titleLabel.text;
    self.reportView.hidden = NO;
}

If you notice this:

thumbnailView.frame = CGRectMake(0, 0, size.width, size.height);

It means I want to change my thumbnail to a big one, so I changed frame.
But with this frame change, when I touch the view, it disappears.
But after I zoom it in or out, it shows again.

The strange thing is: when I remove that frame change, the problem is gone.

I want to know why and how to solve this problem (I still need change frame if possible).

levi
  • 171
  • 2
  • 2
  • 9

2 Answers2

0

Have you tried a call to [thumbnailView layoutSubviews] after you change the frame? I had a similar problem where my views were just disappearing after the frame change until the map was touched/zoomed/whatever. This was the missing link to get autolayout to do its magic.

Update Actually, this may have been a red herring. What seemed to definitively work for me was to manipulate the centerOffset property of the MKAnnotationView rather than its frame directly. Doing things to frame led to weird effects where the origin got "reset" momentarily to 0,0 and only got back to normal when the map was touched. In the end, I used centerOffset, adjusted the frames of subviews instead, and used this solution of hittesting to get interaction in the subviews to work.

Community
  • 1
  • 1
Zoë Smith
  • 1,084
  • 1
  • 11
  • 19
0

This did the trick for me

CGSize size = [self customSize];

if (!CGSizeEqualToSize(size, self.frame.size))
{
    [self setFrame:CGRectMake(0, 0, size.width, size.height)];
}

setting the frame once for all solved any issue, building for iOS7 does't show this problem.