2

I created a class to implement a popover behavior. This class creates a UIWindow and places the view controller inside of it. The view controller is resized to a particular dimension and then the new window is made visible. All is good as long as the device stays in the same orientation. I am having a problem when rotating the device. The view controller's view isn't resized according to the orientation. Code is:

@implementation Popover

- (void)present {
    [self.window addSubview:self.backgroundView];
    self.backgroundView.backgroundColor = NICE_BLACK_COLOR;
    self.window.rootViewController = self.contentViewController;
    [self.window makeKeyAndVisible];
    [self animatePresentation];
}

-(void)animatePresentation {
    CGRect finalFrame = [self viewControllerFrame];
    self.contentViewController.view.frame = [self hiddenFrame];
// trying to set constraints to the view controller's view for when the window rotates. but this isn't working as wanted
    self.contentViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin;
    [UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.7 options:0 animations:^{
        self.contentViewController.view.frame = finalFrame;
    } completion:^(BOOL finished) {
        _visible = YES;
    }];
}
@end

Another option I was considering was to not allow rotation. The way I attempted that was to set a dummy view controller to the UIWindow and disable rotation in it.

@implementation NoRotationViewController

-(BOOL)shouldAutorotate {
    return NO;
}

@end

and set it as the root view controller of the UIWindow.

@implementation Popover
-(void)present {
   NoRotationViewController *vc = [NoRotationViewController new];
   vc.backgroundColor = [UIColor purpleColor];
   self.window.rootViewController = vc;
   [self.window makeKeyAndVisible];
}
@end

Now the popover window doesn't allow rotation but the main window under it still rotates (including the status bar)

Here is the print screen . notice the status bar orientation and original window's view controller (It is a split view controller - notice the master section)

enter image description here

enter image description here

Avba
  • 14,822
  • 20
  • 92
  • 192

0 Answers0