0

我试图实现我自己的 CustomUIActionSheet。

我几乎可以正常工作,但我不知道 showInView 方法是如何工作的。

  • (void)showInView:(UIView *)view

给出一个视图,这个方法能够将它的视图放在每个视图的前面(也许将它添加到窗口中?)但它也能够根据正在添加的视图设置旋转。

我尝试将它添加到我作为参数接收的视图的窗口中。

CGFloat startPosition = view.window.bounds.origin.y + view.window.bounds.size.height;

self.frame = CGRectMake(0, startPosition, view.window.bounds.size.width, [self calculateSheetHeight]);

[view.window addSubview:self];

self.blackOutView = [self buildBlackOutViewWithFrame:view.window.bounds];
[view.window insertSubview:self.blackOutView belowSubview:self];

通过这样做,所有工作,除了当我在横向呈现操作表时,操作表从右侧出现(因为 Windows 系统引用它总是相同的)

我还尝试将它添加到视图窗口的 rootViewController 中,如下所示:

UIView * view = view.window.rootViewController.view;

 CGFloat startPosition = view.bounds.origin.y + view.bounds.size.height;

 self.frame = CGRectMake(0, startPosition, view.bounds.size.width, [self calculateSheetHeight]);

 [view addSubview:self];

 self.blackOutView = [self buildBlackOutViewWithFrame:view.bounds];
 [view insertSubview:self.blackOutView belowSubview:self];

但同样,它在风景中失败了,它没有添加任何东西,或者至少,我看不到它

所以,我的问题是,关于如何在两个方向上将视图添加到层​​次结构顶部的任何线索?

多谢!

4

1 回答 1

1

在阅读了一些自定义警报视图的源代码和在层次结构顶部绘制的其他 ui 组件后,我找到了一个可行的解决方案。

在窗口的第一个子视图中添加视图:

UIView * topView = [[view.window subviews] objectAtIndex:0];

所以最终的代码是

UIView * topView = [[view.window subviews] objectAtIndex:0];

CGFloat startPosition = topView.bounds.origin.y + topView.bounds.size.height;

self.frame = CGRectMake(0, startPosition, topView.bounds.size.width, [self calculateSheetHeight]);

[topView addSubview:self];

self.blackOutView = [self buildBlackOutViewWithFrame:topView.bounds];
[topView insertSubview:self.blackOutView belowSubview:self];
于 2013-03-12T14:29:07.460 回答