Question

I have an MKMapView with an iAd banner on top. Due to this iAd Banner covering the MKMapView's legal label, it is moved. The following code is performed in viewDidLayoutSubviews:

NSLog(@"ViewDidLayoutSubviews called");
UILabel *attributionLabel = [self.mapView.subviews objectAtIndex:1];
if (adBannerViewFrame.origin.y < attributionLabel.frame.origin.y) {
    NSLog(@"Banner Y coordinate: %f", adBannerViewFrame.origin.y);
    CGRect legalFrame = attributionLabel.frame;
    legalFrame.origin.y -= IAD_BANNER_HEIGHT;
    attributionLabel.frame = legalFrame;
    NSLog(@"Legal y: %f", legalFrame.origin.y);
    NSLog(@"Legal x: %f", legalFrame.origin.x);
    NSLog(@"Legal width: %f", legalFrame.size.width);
    NSLog(@"Legal height: %f", legalFrame.size.height);
}

When I switch from the View Controller containing the map to another VC and back again, the code above works perfect, regardless of orientation; however, when rotating the devices without switching VCs, the legal label disappears on rotation and don't come back until I switch back and fourth between VCs again. The NSLogs print out the exact same for both rotation and VC switching:

ViewDidLayoutSubviews called

Banner Y coordinate: 902/646(rotation dependent)

Legal x: 882/626(rotation dependent)

Legal y: 11

Legal width: 48

Legal height 11

Despite everything looking the same, the label is gone after rotating (not sure if it is moved out of the view or if it actually disappears)

The iAd Banner is reloaded in ViewDidAppear (which is called when VCs are switched back and fourth). However, since the NSLogs print the same for both scenarios, I cannot see how it should make any difference. To be sure, I tried to reload the iAd Banner in ViewDidLayoutSubviews as well, with no different result than before. Anyone experienced anything similar or know how to fix this?

Was it helpful?

Solution

For anyone encountering the same issue, I found a solution. The workaround was to schedule a timer with 0 seconds and have the timer call the method executing the code above.

[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(test) userInfo:nil repeats:NO];

I assume that something was not completely finished when viewDIDlayoutSubviews was called and that adding a timer (which is not 100 % accurate and will therefore not start immediately), allowed the unknown remaining stuff to complete before the code in the question was executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top