Reputation: 293
I want to set device orientation to landscape mode programmatically to particular viewcontroller, its working well if I use with UIAlertController, with YES/NO option, but if I directly write the same code on button click. its not working.
working code with UIAlertController,
UIAlertController * alert=[UIAlertController
alertControllerWithTitle:APP_NAME message:@"Please Select Video Orientation."preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"Landscape Mode"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self.navigationController pushViewController:mySecondViewController animated:NO];
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@"Portrait Mode"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self.navigationController pushViewController:mySecondViewController animated:NO];
}];
[alert addAction:yesButton];
[alert addAction:noButton];
[self presentViewController:alert animated:YES completion:nil];
The above code is working well, but i want to force next controller in landscape mode without asking with Alert, so directly write the code on button click like this,
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
[self.navigationController pushViewController:mySecondViewController animated:NO];
but this same code is not working well. it will open the mySecondViewController in portrait first and manually we have to tilt phone once then it will move into landscape.
I tried lot of option but no luck. Testing on iPhone5 and iPhone6 with iOS 10.1
Update: adding mySecondViewController code, This is second view controller code,
-(void)viewWillAppear:(BOOL)animated{
self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidAppear:(BOOL)animated {
// [super viewDidAppear:animated];
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft|UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
-(BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
Upvotes: 0
Views: 1023
Reputation: 2547
Try this for force the view to launch in landscape mode in swift : Use this code in viewWillAppear
let value = UIInterfaceOrientation.LandscapeLeft.rawValue
UIDevice.currentDevice().setValue(Int(value), forKey: "orientation")
and
override func shouldAutorotate() -> Bool {
return true
}
Upvotes: 1
Reputation: 17912
This is code for set orientation, but if you want for specific VC try this code in that VC class
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]
forKey:@"orientation"];
Upvotes: 0