UIView rotated 90 degrees when added to landscape view
I'm working on a custom UIPopoverController
. I'm not subclassing, I'm writing my own from scratch as a NSObject
subclass.
It works quite simply. It adds a fullscreen UIView
to the UIWindow
to intercept touches. Above that view, it adds the (smaller) popover view and it positions the popover at a suitable point.
I've tested it in a portraid iPad app and it works fine. It positions the popover view well.
But now I've encountered a problem. I need to use it in a landscape iPad app, but the view of my popover controll开发者_Python百科er is still displayed portrait rather than landscape. It doesn't autorotate (which, I thought, it should).
I know I can manually rotate the view by setting the transform property. But this will make things really complex, because I will have to recalculate the correct positioning, taking the 90 degree rotation in account. Is there any way around this?
Let's have a look at what the UIPopoverController is doing here. From the documentation:
If the user rotates the device while a popover is visible, the popover controller hides the popover and then shows it again at the end of the rotation. The popover controller attempts to position the popover appropriately for you but you may have to present it again or hide it altogether in some cases.
So the UIPopoverController isn't rotating the popover view that it maintains, like a UIViewController would. This makes sense, considering that UIPopoverController inherits from NSObject, and therefore doesn't inherit the UIViewController's code that handles the rotation.
You're trying to implement your own UIPopoverController (I won't ask why!), and you're also inheriting from NSObject, so you inherit no code that will rotate your views for you. When you add your fullscreen UIView to the UIWindow, the UIView exists within the UIWindow coordinate space. When you rotate the device, the coordinate space of the UIWindow never changes. In order for your view to be rotated and moved to the correct space in landscape mode, you'll have to do this manually by applying a CGSAffineTransform to the transform property of your UIView, perhaps calculating a rotation matrix using an angle derived from the interface orientation of the status bar (this is the sort of thing that a UIViewController would do for you!)
Your custom UIPopoverController could manage the rotation, but it would be easier to get this for free using a UIViewController subclass if possible.
精彩评论