Reputation: 167
I am doing a new project in that I need both landscape and portrait view , I have designed my page. The portrait is working good but how to set landscape in programatic. help me dudes.
Upvotes: 0
Views: 71
Reputation: 2768
this is work for me:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (self.firstViewAppear) {
[self.navigationController setNavigationBarHidden:YES];
CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:duration];
self.navigationController.view.transform = CGAffineTransformIdentity;
self.navigationController.view.transform = CGAffineTransformMakeRotation(M_PI*(90)/180.0);
self.navigationController.view.bounds = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
[UIView commitAnimations];
self.firstViewAppear = NO;
}
self.view.frame = self.view.frame;
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscapeRight;
}
Upvotes: 0
Reputation: 25459
I'm using viewDidLayoutSubviews like that:
-(void)viewDidLayoutSubviews
{
CGRect bounds = self.view.bounds;
if (bounds.size.width > bounds.size.height)
{
// landscape layout
[self.myView setFrame:CGRectMake(10.0f, 80.0f, 330.0f, 318.0f)];
}
else
{
// portrait layout
[[self.myView setFrame:CGRectMake(10.0f, 10.0f, 1500.0f, 318.0f)];
}
}
It works fine for me. I assume that your app respond to orientation changes.
Upvotes: 1
Reputation: 21249
Read Supporting multiple interface orientations. Also, if you do want to force some orientation when iOS decides to use another one, there's no way do it with public API.
Upvotes: 0