我的要求是我的第一个视图控制器仅在纵向模式下打开。当用户转到第二个视图控制器时,我希望控制器处于横向模式,我该怎么办呢
我试过这段代码
1st ViewController.m
- (BOOL)shouldAutorotate
{
returnc YES;
}
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
{
return (interface==UIInterfaceOrientationMaskPortrait);
}
第二个ViewController.m的代码
- (BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
//return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
这对我来说不行。
答案 0 :(得分:0)
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
<强>更新:强>
您可以通过创建UINaviagationController的类别
来完成此操作 .h文件的代码是
@interface UINavigationController (autorotation)
-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
和.m文件的代码是
@implementation UINavigationController (autorotation)
-(BOOL)shouldAutorotate
{
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication].statusBarOrientation;
[self.topViewController shouldAutorotate];
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
@end
答案 1 :(得分:0)
在第二个视图控制器的viewcontroller .m文件中粘贴以下代码(在@implementation部分下)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
现在选择故事板中的第二个视图控制器(选择由视图控制器周围的蓝色边框表示),转到属性检查器(右侧'盾'像图像一样)将方向更改为横向..就是这样..。告诉我如果它不适合你。 ..:)
答案 2 :(得分:0)
我正在使用Category解决我的问题.... 添加新文件并选择Category并创建子类UINavigationController类。
这是.h
的类别代码 #import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface UINavigationController (orientation)
@end
code for .m file
#import "UINavigationController+orientation.h"
@implementation UINavigationController (orientation)
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
if (delegate.islandscape)
{
// for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
@end
在App委托中声明isLandscape以检查天气第一个视图控制器或第二个视图控制器isLandscape是Bool。
现在FirstViewController.m文件我希望在Portarit模式中使用此代码
- (IBAction)PlayClicked:(id)sender
{
AppDelegate * delegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
self.navigationController.navigationBarHidden=YES;
delegate.islandscape=YES;
ViewController * v=[[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
[self presentViewController:v animated:NO completion:nil];
//[self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController pushViewController:v animated:YES];
}
- (NSInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
和SecondViewController我希望在横向模式下使用这个。
delegate.islandscape=NO; // called transfer to Category
- (NSInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}