0

I am using the following code to create a view and put it on top:

UIWindow* mainWindow = [[UIApplication sharedApplication] keyWindow];
CGRect viewRect = mainWindow.frame;
topView = [[UIView alloc] initWithFrame:viewRect];
[topView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4]];
[mainWindow addSubview:topView];

It works perfectly but my problem is if I write anything one the view(like using a Label) and my device is in a landscape position, the text is in vertical position. I've attached a picture to make it more clear. Is there any way to fix it?enter image description here

EDIT: if I use UIApplication.sharedApplication.keyWindow.rootViewController instead of mainWindow, I will get this:

enter image description here

AliBZ
  • 4,039
  • 12
  • 45
  • 67

2 Answers2

0

There's no way to fix it without adding the given view to a View Controller (UIWindow was never meant to handle rotation, and has no logic to do so).

The wonky view rotation results you're experiencing are actually the result of UIView's default autoresizingMask's

topView = [[UIView alloc] initWithFrame:viewRect];
[topView setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.4]];
[topViewsetAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
CodaFi
  • 43,043
  • 8
  • 107
  • 153
0

See this answer:

View on top of everything: UIWindow subview VS UIViewController subview

Basically, only the first subview of the main window gets rotation events, so you have to do it some other way.

Community
  • 1
  • 1
Ned
  • 6,280
  • 2
  • 30
  • 34