Created
March 27, 2012 20:59
-
-
Save bkoell/2220183 to your computer and use it in GitHub Desktop.
MKMapView: relocating Google-Image and Copyright-Label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//example relocation implementation | |
- (void) relocate:(MKMapView*)map { | |
UIImageView *google = [self googleImageForMapView:map]; | |
CGRect googleFrame = CGRectMake(google.frame.origin.x, 10.0, google.frame.size.width, google.frame.size.height); | |
[google setFrame:googleFrame]; | |
UILabel *copyright = [self copyrightLabelForMapView:map]; | |
CGRect copyrightFrame = CGRectMake(copyright.frame.origin.x, 10.0, copyright.frame.size.width, copyright.frame.size.height); | |
[copyright setFrame:copyrightFrame]; | |
} | |
//get single UILabel or nil | |
- (UILabel*) copyrightLabelForMapView:(MKMapView*)mapView { | |
NSArray *labels = [self subviewsOfClass:[UILabel class] containedIn:mapView]; | |
return labels.count>0 ? [labels objectAtIndex:0] : nil; | |
} | |
//get single UIImageView or nil | |
- (UIImageView*) googleImageForMapView:(MKMapView*)mapView { | |
NSArray *images = [self subviewsOfClass:[UIImageView class] containedIn:mapView]; | |
return images.count>0 ? [images objectAtIndex:0] : nil; | |
} | |
//find all subviews of a given class (recursive-> finds sub..sub..subviews until there are none!) | |
- (NSArray*) subviewsOfClass:(Class)class containedIn:(UIView*)rootView { | |
NSMutableArray *matchingSubviews = [[[NSMutableArray alloc] init] autorelease]; | |
for (UIView *view in [rootView subviews]) { | |
if ([view isKindOfClass:class]) { | |
[matchingSubviews addObject:view]; | |
} | |
NSArray *matchingSubviewsFromSubcall = [self subviewsOfClass:class containedIn:view]; | |
[matchingSubviews addObjectsFromArray:matchingSubviewsFromSubcall]; | |
} | |
return matchingSubviews; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment