2013-10-02 1 views
1

하위 뷰를 뷰에 추가 할 때 트리거되는 대리자 메서드를 찾고 있습니다. iOS7지도의 나침반보기가 회전하기 시작할 때 Mapview에 추가되기 때문에 필요합니다 (이전에 표시되지 않고 숨김 상태가 아닙니다). 그러나 regionWillChangeAnimated에서 나침반보기를 조작하려고 시도하면 사용자가지도를 놓을 때까지 트리거되지 않습니다.subviewsDidChange 대리자 메서드를 찾고

기본적으로 나는 subviewWasAdded, subviewsDidChange 또는 그와 비슷한 내용이 필요합니다.

EDIT : Daij-Djan의 제안을 받아서 MPOMapView.m 및 MPOMapView를 만들었습니다.

#import "MPOMapView.h" 

@implementation MPOMapView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
    } 
    return self; 
} 

- (void)addSubview:(UIView *)view { 
    [super addSubview:view]; 

    [self moveCompass:&view]; 
} 


- (void)moveCompass:(UIView **)view { 

    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7) { 
     return; 
    } 

    if(![*view isKindOfClass:NSClassFromString(@"MKCompassView")]) { 
     return; 
    } 

    //Gets here 
    NSLog(@"Was the compass"); 

    float width = (*view).frame.size.width; 
    float height = (*view).frame.size.height; 

    (*view).frame = CGRectMake(40, self.frame.size.height - 50, width, height); 

    //Frame doesn't change 
    NSLog(@"Frame should change"); 
} 

@end 

이 메서드는 호출되지만 프레임은 변경되지 않습니다. 나는 또한 시도했다 :

- (void)addSubview:(UIView *)view { 
    [super addSubview:view]; 

    [self moveCompass:view]; 
} 


- (void)moveCompass:(UIView *)view { 

    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7) { 
     return; 
    } 

    if(![view isKindOfClass:NSClassFromString(@"MKCompassView")]) { 
     return; 
    } 

    //Gets here 
    NSLog(@"Was the compass"); 

    float width = view.frame.size.width; 
    float height = view.frame.size.height; 

    view.frame = CGRectMake(40, self.frame.size.height - 50, width, height); 

    //Frame doesn't change 
    NSLog(@"Frame should change"); 
} 

그러나 이것도 작동하지 않는다. 그리고 마지막으로 ....

- (void)addSubview:(UIView *)view { 
    [super addSubview:[self movedCompass:view]]; 
} 


- (UIView *)movedCompass:(UIView *)view { 

    if([[[UIDevice currentDevice] systemVersion] floatValue] < 7) { 
     return view; 
    } 

    if(![view isKindOfClass:NSClassFromString(@"MKCompassView")]) { 
     return view; 
    } 

    //Gets here 
    NSLog(@"Was the compass"); 

    float width = view.frame.size.width; 
    float height = view.frame.size.height; 

    view.frame = CGRectMake(40, self.frame.size.height - 50, width, height); 

    //Frame doesn't change 
    NSLog(@"Frame should change"); 

    return view; 
} 

는 그러한 위임이없는 중 하나

+0

@Mike_V 당신은 당신의 앱이 MKCompassView private 클래스 그런 식으로 액세스, 코드를 통해 허용 얻을 관리 했습니까? 공공 응용 프로그램에 대해 동일한 작업을 수행해야합니다. – pnizzle

답변

2

작동하지 않습니다.

하지만

모든 당신이 할 수있는 서브 클래스 MKMapView하고 insertSubview 및 addSubview를 구현하고 자신의 솔루션을 구축 할 수 있습니다.

하지만

- (void)layoutSubviews { 
    [super layoutSubviews]; 

       if([[[UIDevice currentDevice] systemVersion] floatValue] < 7) { 
        return; 
       } 

    for(id view in self.subviews) { 
       if(![view isKindOfClass:NSClassFromString(@"MKCompassView")]) { 
        continue; 
       } 

       [self moveCompass:view]]; 
    } 
} 

- (void)moveCompass:(UIView *)view { 
    //Gets here 
    NSLog(@"Was the compass"); 

    float width = view.frame.size.width; 
    float height = view.frame.size.height; 

    //view.frame = CGRectMake(40, self.frame.size.height - 50, width, height); 
    view.frame = CGRectMake(40, self.bounds.size.height - 50, width, height); 

} 
+0

나는 이것을 시험해보고 대답을 받아 들일 것이다. 감사! –

+0

내 편집 –

+0

아를 참조하십시오. 내가하고 싶은 것을 보았습니다. :) addSubview의보기에 대한 포인터를 저장하고 layoutViews를 구현하십시오 ... 내 코드를 초 단위로 편집하십시오. –

0

가 작동하는지 모르겠어요, 당신은지도보기의 camera하는 heading 속성이있는 관찰 키 값을 사용하여 시도 할 수 구입할 더 나은 방법을 참조하십시오.

+0

시도 [self.mapView.camera addObserver : self forKeyPath : @ "heading"옵션 : NSKeyValueObservingOptionNew | NSLogValueObservingOptionOld 컨텍스트 : nil]; 및 - (void) observeValueForKeyPath : (NSString *) keyPath ofObject : (id) 개체 변경 : (NSDictionary *) 컨텍스트 변경 : (void *) 컨텍스트 { NSLog (@ "키 경로 : % @ ", keyPath); }'아무 것도 인쇄되지 않습니다 –

+0

시도해 주셔서 감사합니다. 너무 좋지는 않았지만 맵 카메라에는 KVO가 없다는 것을 알았습니다. –

2

Daij-Djan이 정답을 게시했습니다.

이 내가 어떤 느낌 on 단지 개선이 너무 많은 코드입니다 :

- (void)layoutSubviews 
{ 
    [super layoutSubviews]; 

    for(id view in self.subviews) 
    { 
     if([view isKindOfClass:NSClassFromString(@"MKCompassView")]) 
      [self moveCompass:view]]; 
    } 
} 

- (void)moveCompass:(UIView *)view 
{ 
    //change compass frame as shown by Daij-Djan 
} 
관련 문제