我正在尋找某種委託方法,將子視圖添加到視圖時會觸發。我需要這個,因爲iOS7地圖上的指南針視圖在開始旋轉時添加到mapview中(它以前不存在且未隱藏)。但是,當我嘗試操作regionWillChangeAnimated
中的指南針視圖時,它不會觸發,直到用戶放開地圖。尋找子視圖ChangeChange委託方法
基本上我需要沿線的任何東西: subviewWasAdded
,subviewsDidChange
,或類似的東西。
編輯:經過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;
}
無法正常工作或
@Mike_V沒有你設法讓你的應用接受與您的代碼訪問MKCompassView私有類呀?我需要爲公共應用程序做同樣的事情。 – pnizzle