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

问题描述 投票:0回答:1

我需要在视图内部需要推送其他视图控制器。为此,我尝试添加一个窗口作为该特定视图的子视图,然后我已将所需的视图控制器提供给该窗口的根视图控制器。它显示正确,但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没有正确设置。

ios objective-c uiwindow
1个回答
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]; 
© www.soinside.com 2019 - 2024. All rights reserved.