我正在寻找将子视图添加到视图时触发的某种委托方法。我需要这个,因为 iOS7 地图上的指南针视图在它开始旋转时被添加到地图视图中(它以前不存在并且未隐藏)。但是,当我尝试在其中操作指南针视图时,regionWillChangeAnimated
它不会触发,直到用户放开地图。
基本上我需要任何类似的东西:
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;
}
也不起作用