Skip to content

Instantly share code, notes, and snippets.

@xavierjurado
Created November 7, 2015 14:25
Show Gist options
  • Save xavierjurado/36831db07b55e0e3479f to your computer and use it in GitHub Desktop.
Save xavierjurado/36831db07b55e0e3479f to your computer and use it in GitHub Desktop.
On how MKAnnotationView works with autolayout
//
// ViewController.m
// AnnotationHell
//
// Created by Xavier Jurado on 07/11/15.
// Copyright © 2015 Xavier Jurado. All rights reserved.
//
#import <MapKit/MapKit.h>
#import <Masonry/Masonry.h>
#import "ViewController.h"
#define FerHomeCoordinate2D CLLocationCoordinate2DMake(34.1170966, -118.2561648)
#pragma mark - Model
@interface Annotation : NSObject <MKAnnotation>
@end
@implementation Annotation
- (CLLocationCoordinate2D)coordinate
{
return FerHomeCoordinate2D;
}
@end
#pragma mark - View
@interface AnnotationView : MKAnnotationView
@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) UILabel *bottomLabel;
@property (nonatomic, strong) UIImageView *imageView;
@end
@implementation AnnotationView
- (instancetype)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.translatesAutoresizingMaskIntoConstraints = NO;
_topLabel = [[UILabel alloc] init];
_topLabel.translatesAutoresizingMaskIntoConstraints = NO;
_topLabel.text = @"Fer & Lena's home";
_bottomLabel = [[UILabel alloc] init];
_bottomLabel.translatesAutoresizingMaskIntoConstraints = NO;
_bottomLabel.text = @"💑";
_imageView = [[UIImageView alloc] init];
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
_imageView.image = [UIImage imageNamed:@"fer.jpeg"];
_imageView.clipsToBounds = YES;
[self addSubview:_topLabel];
[self addSubview:_bottomLabel];
[self addSubview:_imageView];
self.backgroundColor = [UIColor whiteColor];
self.layer.shadowOpacity = 0.3;
[self setupLayout];
}
return self;
}
- (void)setupLayout
{
[self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left);
make.top.equalTo(self.mas_top);
make.bottom.equalTo(self.mas_bottom);
make.size.mas_equalTo(CGSizeMake(50, 50));
}];
[self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.mas_top).with.offset(2);
make.left.equalTo(self.imageView.mas_right).with.offset(5);
make.right.equalTo(self.mas_right).with.offset(-5);
}];
[self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self.mas_bottom).with.offset(-2);
make.left.equalTo(self.imageView.mas_right).with.offset(5);;
make.right.equalTo(self.mas_right).with.offset(-5);
}];
}
@end
#pragma mark - Controller
@interface ViewController () <MKMapViewDelegate>
@property (nonatomic, weak) IBOutlet MKMapView *mapView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView addAnnotation:[Annotation new]];
[self.mapView showAnnotations:self.mapView.annotations animated:NO];
}
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
return [[AnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"AnnotationView"];
}
@end
@mattbezark
Copy link

that's very helpful. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment