My application was not made for the landscape. But when I open my YouTube channel in a UIWebView and a user launches a video, it appears in Portrait. I would like to make it appear in landscape mode if the user rotate his iPhone.
How to enable landscape mode as in this case?
I know there are "dirty hacks" to do this but I prefer something cleaner. Also I do not want the UIWebView switches to landscape but just that the videos can.
Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off.
I finally adjusted my view so that it supports landscape mode using the following code :
- (void)viewDidLoad {
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)viewWillAppear:(BOOL)animated {
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation))
{
//I set my new frame origin and size here for this orientation
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait)
{
//I set my new frame origin and size here for this orientation
isShowingLandscapeView = NO;
}
}
- (void)orientationChanged:(NSNotification *)notification
{
// We must add a delay here, otherwise we'll swap in the new view
// too quickly and we'll get an animation glitch
[self performSelector:@selector(updateLandscapeView) withObject:nil afterDelay:0];
}
- (void)updateLandscapeView
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) && !isShowingLandscapeView)
{
//I set my new frame origin and size here for this orientation
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait && isShowingLandscapeView)
{
//I set my new frame origin and size here for this orientation
isShowingLandscapeView = NO;
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait || UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With