如何在屏幕的某些部分添加另一个UIWindow

时间:2018-02-22 07:55:20

标签: ios objective-c uiwindow

我在视图中需要推送一些其他视图控制器。为此,我尝试添加一个窗口作为该特定视图的子视图,然后我已将所需的视图控制器提供给该窗口的根视图控制器。它显示正确,但rootviewcontroller从屏幕(0,0)点开始,而不是在窗口内。

enter image description here

在此图片中,红色是视图控制器,灰色是具有原点(0,100)的视图。以下是我尝试的代码。

UIWindow *insideWindow = [[UIWindow alloc] initWithFrame: CGRectMake(0, 100, _windowView.frame.size.width, _windowView.frame.size.height)];

    insideWindow.backgroundColor = [UIColor yellowColor];
    insideWindow.windowLevel = UIWindowLevelNormal;
    insideWindow.hidden = NO;
    insideWindow.clipsToBounds = YES;
    insideWindow.bounds = _windowView.bounds;
    _windowView.clipsToBounds = YES;

    [_windowView addSubview: insideWindow];

SecondViewController *s = [SecondViewController new];
        s.view.bounds = CGRectMake(0, 100, _windowView.frame.size.width, _windowView.frame.size.height);

        insideWindow.rootViewController = [SecondViewController new];
         [insideWindow makeKeyAndVisible];

如何添加多个UIWindows并不重复。在这里,我可以添加多个窗口但问题是在窗口内,viewcontrollers没有正确设置。

1 个答案:

答案 0 :(得分:0)

我真的不知道你想要达到什么,但我们走了......

SecondViewController *s = [SecondViewController new];
s.view.bounds = CGRectMake(0, 100, _windowView.frame.size.width, _windowView.frame.size.height);
insideWindow.rootViewController = [SecondViewController new];

你基本上创建了一个SecondViewController的实例,然后你设置了边界(错误的想法),然后你指定为窗口rootViewController的另一个SecondViewController实例。 视图的框架和边界之间的差异:Bounds vs Frame

如果你想这样做,那么:

SecondViewController *s = [SecondViewController new];
s.view.frame = CGRectMake(0, 100, _windowView.frame.size.width, _windowView.frame.size.height);
insideWindow.rootViewController = s;

而不是添加窗口来显示视图控制器,为什么不将“SecondViewController”作为子项添加到具有“灰色”视图的控制器?

SecondViewController *s = [SecondViewController new];
[firstViewController addChildViewController:s];
s.view.frame = grayView.bounds; // or CGRectMake(...)
[grayView addSubview:s.view];
[s didMoveToViewController:firstViewController];