gpt4 book ai didi

ios - 寻找 subviewsDidChange 委托(delegate)方法

转载 作者:行者123 更新时间:2023-12-01 18:18:39 25 4
gpt4 key购买 nike

我正在寻找将 subview 添加到 View 时触发的某种委托(delegate)方法。我需要这个,因为 iOS7 map 上的指南针 View 在它开始旋转时被添加到 map View 中(它以前不存在并且未隐藏)。但是,当我尝试在 regionWillChangeAnimated 中操作指南针 View 时在用户放开 map 之前它不会触发。

基本上我需要以下任何东西: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;
}

也不起作用

最佳答案

没有这样的代表。



您所能做的就是继承 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);

}

关于ios - 寻找 subviewsDidChange 委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19146345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com