我有一个仅支持横向模式的应用程序。当我打开相机时,应用程序崩溃了。我使用以下代码解决了这个问题。
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate {
return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait;
}
但该应用程序转而支持所有方向。我想要的只是应用程序只支持横向模式,也使用相机。
任何帮助将不胜感激。
答案 0 :(得分:1)
我终于明白了。在ViewControllers中永远不会调用shouldAutorotate。因为最顶层的视图控制器必须返回它想要旋转的界面方向。就我而言,这就是UINavigationController。所以我为UINavigationController创建了一个新类别,使其工作。我在类别
中使用以下方法@implementation UINavigationController (BugiOSFix)
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate {
return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait;
}
@end
答案 1 :(得分:0)
写这个方向.........
-(void)viewWillAppear:(BOOL)animated
{
[self willAnimateRotationToInterfaceOrientation:
[UIApplication sharedApplication].statusBarOrientation duration:1.0];
UIInterfaceOrientation orientation =
[UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeRight ||
orientation == UIInterfaceOrientationLandscapeLeft)
{
[self setFrameForLeftAndRightOrientation];
}
else if(orientation == UIInterfaceOrientationPortrait )
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
- (void)willAnimateRotationToInterfaceOrientation:
(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
[self setFrameForLeftAndRightOrientation];
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
[self setFrameForPortraitAndUpsideDownOrientation];
}
}
-(void)setFrameForLeftAndRightOrientation
{
if(IS_IPAD)
{
// Write here for left and right orientation
}
else if(is_iPhone)
{
// Write here for iPhone land scape ;
}
-(void)setFrameForPortraitAndUpsideDownOrientation
{
if(IS_IPAD)
{
// Write here for Portrait orientation for iPad
}
else if(is_iPhone)
{
// Write here for iPhone Portrait orientation ;
}
}
答案 2 :(得分:0)
在How to create a landscape-view only ios application查看我的回答,提出类似的问题。您所要做的就是实现一种方法,您的应用将被迫自动旋转到您想要的方向。
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft;
}
这会强制您的应用向UIInterfaceOrientationLandscapeLeft
方向旋转,只需为您的ViewController提供一个首选方向。