重新定位法律标签(MKAttributionLabel)

时间:2015-06-09 14:59:51

标签: ios iphone ios8 mkmapview mapkit

我想将Legal标签移到右侧。在iOS 6和7上,下面的解决方案工作正常,但是在iOS 8.3上似乎无法正常工作。

我得到标签,然后在viewDidLayoutSubviews中使用计时器(0.1秒)我称之为:

-(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;
}

适用于旋转等 但是一旦我滚动地图,标签就会跳回到左边。 试图在regionDidChangeAnimated中调用此方法,但标签首先向左跳,然后向右跳,它真的很烦人......

我怎么能强迫那个愚蠢的标签留在右边?

Christian建议的解决方案:

  1. 对MKMapView进行子类化
  2. moveLegalLabel代码移到那里
  3. layoutSubviews
  4. 中调用它

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

7 个答案:

答案 0 :(得分:8)

你需要它在viewDidAppear和两个MKMapViewDelegate方法中。如果你没有将它放在两个委托方法中,你将获得你所看到的跳跃。就这样!

-(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;
}

答案 1 :(得分:3)

像其他人建议的-viewDidAppear:-mapView:regionWillChangeAnimated:-mapView:regionDidChangeAnimated:之类的钩子不适用于此目的。

您最好的选择是继承MKMapView,并在<{strong>>调用-layoutSubviews之后在super 中布置您的标签。

答案 2 :(得分:3)

Swift 4示例:

  • 在iOS 11上运行

  • 不需要子类

(基于Erik的回答)

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)
}

答案 3 :(得分:1)

您必须先在viewDidAppear中加载代码,然后才能执行代码。显然你的代码是在视图准备好之前编译的。

-(void)moveLegalLabel
{
    UIView * legalLink = _mapView.subviews[1];
    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;
}


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

如果您创建了一个按钮并将代码置于其操作中,则单击时您可以看到标签移动。

答案 4 :(得分:1)

尝试以这种方式使用 /tmp/java_H98cOI/ComputeAreaWConsoleInput.java:8: error: ';' expected Scanner sc = New Scanner(System.in); ^ 1 error

transform

如果不行,请尝试使用- (void)moveLegalLabel { UIView *legalLink = [self attributionView]; legalLink.transform = CGTransformMakeTranslation(self.mapView.frame.size.width - CGRectGetMaxX(legalLink.frame) - CGRectGetMinX(legalLink.frame), self.mapView.frame.size.height - CGRectGetMaxY(legalLink.frame) - CGRectGetMinY(legalLink.frame)); }

layer.transform

答案 5 :(得分:0)

我把它从底部移到顶部:

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    // Move legal notice since covered in design
    mapView.subviews
        .first { "\(type(of: $0))" == "MKAttributionLabel" }?
        .frame.origin.y = mapView.frame.minY + 10
}

答案 6 :(得分:0)

要解决此问题,您可以:

1.Create Subclass(MKMapView)

class CustomMapView: MKMapView {

}

2.Override layouSubviews像这样:

override func layoutSubviews() {
        super.layoutSubviews()
        let legalLabel: UIView = self.subviews[1]
        let frame = .....
    }

这种方式对我有用