0

I have requirement like inside the view I need to push some other view controllers. For that I tried to add a one window as subview for that particular view then I have given the desired view controller to root view controller for that window. It is showing correctly but the rootviewcontroller is starting from screen (0, 0) point instead of within the window.

enter image description here

In this picture red color is the view controller and gray one is the view with the origin (0, 100). Below is the code I have tried.

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];

It is not a duplicate with how to add multiple UIWindows. Here I can able to add the multiple windows But the problem is inside window the viewcontrollers are not setting properly.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
  • Possible duplicate of [Add multiple UIWindow](https://stackoverflow.com/questions/19315559/add-multiple-uiwindow) – Saad Chaudhry Feb 22 '18 at 08:02
  • @SaadChaudhry It may not the duplicate question. Can you please check my expiation in the question. – Vishnuvardhan Feb 22 '18 at 08:08
  • Possible duplicate does not means that it's an exact word to word duplicate but in the other question or answer you may find relevant solution to work around. – Saad Chaudhry Feb 22 '18 at 08:12
  • Are you sure you want to add a UIWindow and not simply a controller pushed modally, or even a view ? I think you're headed in the wrong direction with the UIWindow ; why do you *need* it to be that, and not something else ? – Gil Sand Feb 22 '18 at 08:18
  • @GilSand thanks for the difference direction. Indeed I am using some third party framework there I have only access for the view, In that space I want to push few view controllers that's why I have chosen that way. But it is not working as expected. – Vishnuvardhan Feb 22 '18 at 08:58
  • @SaadChaudhry Did u find answer for my question in tagged question? – Vishnuvardhan Feb 22 '18 at 09:00
  • You can add child view controllers to other view controllers. I don't see why a second UIWindow is necessary or relevant here. – Connor Neville Feb 22 '18 at 18:01

1 Answers1

0

I don't really know what you want to achieve but here we go...

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

You basically create an instance of SecondViewController, then you set the bounds (wrong idea) then you assign as window rootViewController another instance of SecondViewController. The difference between the frame and bounds of a view: Bounds vs Frame

If you want to go that way, then:

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

Instead of adding windows to display view controllers why you don't add the "SecondViewController" as child to the controller which has the "gray" view?

SecondViewController *s = [SecondViewController new];
[firstViewController addChildViewController:s];
s.view.frame = grayView.bounds; // or CGRectMake(...)
[grayView addSubview:s.view];
[s didMoveToViewController:firstViewController]; 
Said Hasanein
  • 320
  • 1
  • 5
  • totally different from iOS 13 onwards https://noahgilmore.com/blog/uiwindowscene-black-screen/ – Fattie Jun 09 '22 at 17:01