【发布时间】:2014-01-29 13:02:50
【问题描述】:
我有一个父 UIView 有几个子视图。
子视图是兄弟 - 子视图不是彼此的子视图。 (每个人在他们的superview中处于相同的层次结构)
例如:
UIView *containerView = [[UIView alloc] initWithFrame:frame];
CustomUIView *childView = [[UIView alloc] initWithFrame:anotherFrame];
CustomUIView *yetAnotherChildView = [[UIView alloc] initWithFrame:anotherFrame];
[containerView addSubview:childView];
[containerView addSubview:yetAnotherChildView];
[containerView bringSubviewToFront:childView];
我希望yetAnotherChildView 能够检测到它已移回视图层次结构中。如何做到这一点?
编辑:
我知道containerView 可以知道哪些子视图高于什么 - 但我不想(不能)containerView 通知其子视图他们的顺序已更改 - 想象一下在stock view - stock view 不知道它的子视图,也不知道他们想收到这样的通知。
子视图CustomUIView 需要观察其superview 的一些变化
例如(这可能是一个非常糟糕的解决方案)
CustomUIView
-(id)initWithFrame
{
[self.superView addObserver:self forKeyPath:@"subViews" options:options context:context];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// if keyPath is "subViews" array check if "this" view is ontop and adjust self accordingly
}
【问题讨论】:
标签: ios objective-c uiview uiview-hierarchy