我意识到有很多与此问题有关的问题,但是我还没有找到一个可以解决我的问题的问题。
首先,我在 menu.h 中设置了此代码
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES; }
状态栏随方向而变化,但视图不会旋转或调整大小。为了尝试缩小我的问题,我决定尝试根据方向在一个 .xib 中切换两个视图
- (void)viewDidLoad {
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
-(void) orientationChanged:(NSNotification *)object
{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIDeviceOrientationLandscapeLeft || deviceOrientation == UIDeviceOrientationLandscapeRight)
{
self.view = self.landscapeView;
}
else
{
self.view = self.portraitView;
}
}
在 iOS 模拟器中,视图肯定会更改为特定视图。然而,横向视图显示为纵向和横向。
我在 IB 的风景
更改方向后我在 iOS 模拟器中的横向视图
我在这里做错了什么?我无法弄清楚,任何帮助将不胜感激。先感谢您。** 编辑:下面的新代码 ** 好的.. 我的问题是视图在横向中正确加载,它只是横向的。所以横向视图加载,只是横向。我基于@Seega 修改的代码是:
- (void)viewDidLoad {
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
[[TTNavigator navigator] setCustomTarget:self];
[[TTNavigator navigator] setCustomAction:@selector(photoAction)];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIDeviceOrientationLandscapeLeft || toInterfaceOrientation == UIDeviceOrientationLandscapeRight)
{
self.view = self.landscapeView;
}
else
{
self.view = self.portraitView;
}
}
-(void) orientationChanged:(NSNotification *)object
{
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if (deviceOrientation == UIDeviceOrientationLandscapeLeft || deviceOrientation == UIDeviceOrientationLandscapeRight)
{
self.view = self.landscapeView;
}
else
{
self.view = self.portraitView;
}
}