我正在寻找将子视图添加到视图时触发的某种委托方法。我需要这个,因为iOS7地图上的罗盘视图在它开始旋转时会被添加到mapview中(它不存在以前和未被隐藏)。但是,当我尝试操纵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;
}
不起作用
答案 0 :(得分:2)
没有这样的代表。
BUT
你所能做的只是继承MKMapView并实现insertSubview和addSubview并构建你自己的解决方案。
BUT
看到更好的方式
- (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);
}
答案 1 :(得分:2)
Daij-Djan发布了正确的答案,归功于他!
这只是我觉得代码太多的改进了:
- (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
}
答案 2 :(得分:0)
我不知道它是否会起作用,您可以尝试使用具有camera
属性的mapview heading
使用键值观察。