Reputation: 87
I need to fix orientation portrait or landscapeLeft like Youtube's full screen. When a user click button, it was changed portrait or landscapeLeft. and was fixed. The user can control only by button.
accept Device orientation Portrait, Landscape Left in General It's my code
AppDelegate
-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskLandscapeLeft;
}
ViewController
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate->restrictRotation = restriction;
}
- (IBAction)rotateOrientationAction:(id)sender
{
[self restrictRotation:NO];
if(isPortrait){
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
self.myNewScrollViewHeight.constant = self.view.frame.size.height - self.naviBar.frame.size.height - self.horizMenu.frame.size.height;
self.portraitMenuView.hidden = YES;
}else{
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
self.myNewScrollViewHeight.constant = self.view.frame.size.height * 0.5;
self.portraitMenuView.hidden = NO;
}
isPortrait = !isPortrait;
[self restrictRotation:YES];
}
If I click the button, it was changed landscapeLeft but not changed portrait again.
Thank you
Upvotes: 0
Views: 220
Reputation: 1906
Your condition is not satisfied for restrictRotation
, please check below code to desire result.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
isPortrait = YES;
}
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
- (IBAction)rotateOrientationAction:(id)sender
{
if (isPortrait) {
[self restrictRotation:NO];
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
} else {
[self restrictRotation:YES];
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
}
isPortrait = !isPortrait;
}
Update :
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
isPortrait = YES;
}
- (IBAction)rotateOrientationAction:(id)sender
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = NO;
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
if (isPortrait)
{
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
}
else
{
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
}
isPortrait = !isPortrait;
}
Upvotes: 1