Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reposition legal label ( MKAttributionLabel )

I'd want to move the Legal label to the right side. On iOS 6 and 7 the below solution was working fine, however on iOS 8.3 it seems to not work.

I get the label, then with a timer (0.1 sec) in viewDidLayoutSubviews I call this method :

-(void)moveLegalLabel
{
    UIView * legalLink = [self attributionView];
    legalLink.frame = CGRectMake(self.mapView.frame.size.width - legalLink.frame.size.width - 10, self.mapView.frame.size.height - legalLink.frame.size.height - 10 , legalLink.frame.size.width, legalLink.frame.size.height);
    legalLink.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
}

which works nicely for rotation, etc. But as soon as I scroll the map, the label jumps back to the left. Tried to call this method in the regionDidChangeAnimated but the label jumps back first to the left then to right, it's really annoying...

How could I force that stupid label to stay on the right side ?

Solution as suggested by Christian :

  1. Subclass the MKMapView
  2. Move the moveLegalLabel code there
  3. Call it in layoutSubviews

-(void)layoutSubviews { [super layoutSubviews]; [self moveLegalLabel]; }

like image 253
Templar Avatar asked Jun 09 '15 14:06

Templar


2 Answers

You need it to be in viewDidAppear and both of the MKMapViewDelegate methods. If you don't put it in both delegate methods you will get the jumping that you saw. That's all!

-(void)viewDidAppear:(BOOL)animated
{
    [self moveLegalLabel];
}

- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    [self moveLegalLabel];
}
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    [self moveLegalLabel];
}

-(void)moveLegalLabel
{
    UIView * legalLink = [self.map.subviews objectAtIndex:1];
    legalLink.frame = CGRectMake(self.map.frame.size.width - legalLink.frame.size.width - 10, self.map.frame.size.height - legalLink.frame.size.height - 10 , legalLink.frame.size.width, legalLink.frame.size.height);
    legalLink.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
}
like image 132
Erik Avatar answered Sep 24 '22 20:09

Erik


A Swift 4 example:

  • Working on iOS 11

  • No need for sub-classes

(based off of Erik's answer)

func viewDidAppear(_ animated: Bool) {
   moveLegalLabel()
}

func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
   moveLegalLabel()
}

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
   moveLegalLabel()
}

func moveLegalLabel() {
   let legalLabel: UIView = mapView.subviews[1]
   legalLabel.frame=CGRect(x: mapView.frame.size.width - (legalLabel.frame.size.width ?? 0.0) - 10, y: mapView.frame.size.height - (legalLabel.frame.size.height ?? 0.0) - 10, width: legalLabel.frame.size.width ?? 0.0, height: legalLabel.frame.size.height ?? 0.0)
}
like image 22
George Avatar answered Sep 20 '22 20:09

George