Wilhelm Michaelsen
Wilhelm Michaelsen

Reputation: 663

UIDevice's beginGeneratingDeviceOrientationNotifications doesn't work well

I have a subview in a subclass of UIView. When the device is rotated I want the subview to remove. This is my code:

- (void)awakeFromNib
{
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];

}

- (void)orientationChanged
{
    [subview removeFromSuperview];
}

My problem is that even if the device is tilted a little bit, or put on for example a table the subview removes. What can I do to prevent this?

Upvotes: 4

Views: 1963

Answers (1)

rob mayoff
rob mayoff

Reputation: 385840

Don't track the device orientation. Track the interface orientation. Use UIApplicationWillChangeStatusBarOrientationNotification instead of UIDeviceOrientationDidChangeNotification, and get the new interface orientation out of the notification's userInfo. (You won't have to do beginGeneratingDeviceOrientationNotifications if you track interface orientation.)

Upvotes: 6

Related Questions