iOS (iPhone/iPad) SDK - 应用程序不改变方向

Posted

技术标签:

【中文标题】iOS (iPhone/iPad) SDK - 应用程序不改变方向【英文标题】:iOS (iPhone/iPad) SDK - Application not changing orientation 【发布时间】:2011-08-06 23:15:50 【问题描述】:

在我的自定义标签栏应用程序中,方向似乎永远不会改变,即使我强制旋转状态栏。这是我在 AppDelegate 中的代码:

AppDelegate.h:

    #import <UIKit/UIKit.h>
    #import "MBProgressHUD.h"

    @class exampleViewContoller;
    @class example1ViewController;
    @class example2ViewController;
    @class example3ViewController;
    @class example4ViewController;

    @interface <appname>AppDelegate : NSObject <UIApplicationDelegate, MBProgressHUDDelegate> 
        UIWindow *window;
        UITabBarController *rootController;
        exampleViewContoller *viewController;
        example1ViewController *viewController1;
        example2ViewController *viewController2;
        example3ViewController *viewController3;
        example4ViewController *viewController4;
        NSMutableData *responseData;
        NSMutableArray *tweets;
        MBProgressHUD *HUD;
    

    @property (nonatomic, retain) IBOutlet UIWindow *window;
    @property (nonatomic, retain) IBOutlet UITabBarController *rootController;
    @property (nonatomic, retain) IBOutlet exampleViewContoller *viewController;
    @property (nonatomic, retain) IBOutlet example1ViewController *viewController1;
    @property (nonatomic, retain) IBOutlet example2ViewController *viewController2;
    @property (nonatomic, retain) IBOutlet example3ViewController *viewController3;
    @property (nonatomic, retain) IBOutlet example4ViewController *viewController4;
    @property (nonatomic, retain) NSMutableArray *tweets;

    @end

AppDelegate.m:

    #import "<appname>AppDelegate.h"
    #import "exampleViewContoller.h"
    #import "example1ViewController.h"
    #import "example2ViewController.h"
    #import "example3ViewController.h"
    #import "example4ViewController.h"
    #import "SBJson.h"
    #define TMP NSTemporaryDirectory()

    @implementation <appname>AppDelegate

    @synthesize window = _window;
    @synthesize rootController;
    @synthesize viewController;
    @synthesize viewController1;
    @synthesize viewController2;
    @synthesize viewController3;
    @synthesize viewController4;
    @synthesize tweets;

    #pragma mark -
    #pragma mark Application lifecycle

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    
        CGFloat width = self.rootController.view.bounds.size.width;
        CGFloat height = self.rootController.view.bounds.size.height;
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
        UIImage *imageView = [UIImage imageNamed:@"TabBarBackground.png"];
        UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];

        [v setBackgroundColor:kMainColor];
        [kMainColor release];

        [self.rootController.tabBar insertSubview:v atIndex:0];
        [imageView release];
        [v release];

        responseData = [[NSMutableData data] retain];
        tweets = [NSMutableArray array];
        NSURLRequest *request = [NSURLRequest requestWithURL:
                         [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json?screen_name=ENTER_USER_HERE&count=20"]];
        [[NSURLConnection alloc] initWithRequest:request delegate:self];

        NSAssert(nil != self.rootController, @"tab bar controller not hooked up!");

        BOOL iPad = NO;
#ifdef UI_USER_INTERFACE_IDIOM
        iPad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
#endif

        if (iPad) 
            self.viewController = [[[exampleViewContoller alloc] initWithNibName:@"exampleViewController_iPad" bundle:nil] autorelease];
            self.viewController1 = [[example1ViewController alloc] initWithNibName:@"example1ViewController_iPad" bundle:nil] autorelease];
            self.viewController2 = [[[example2ViewController alloc] initWithNibName:@"example2ViewController_iPad" bundle:nil] autorelease];
            self.viewController3 = [[[example3ViewController alloc] initWithNibName:@"example3ViewController_iPad" bundle:nil] autorelease];
            self.viewController4 = [[[example4ViewController alloc] initWithNibName:@"example4ViewController_iPad" bundle:nil] autorelease];
         else 
            self.viewController = [[[exampleViewContoller alloc] initWithNibName:@"exampleViewContoller_iPhone" bundle:nil] autorelease];
            self.viewController1 = [[[example1ViewController alloc] initWithNibName:@"example1ViewController_iPhone" bundle:nil] autorelease];
            self.viewController2 = [[[example2ViewController alloc] initWithNibName:@"example2ViewController2_iPhone" bundle:nil] autorelease];
            self.viewController3 = [[[example3ViewController alloc] initWithNibName:@"example3ViewController_iPhone" bundle:nil] autorelease];
            self.viewController4 = [[[example4ViewController alloc] initWithNibName:@"example4ViewController_iPhone" bundle:nil] autorelease];
        
            self.rootController.viewControllers = [NSArray arrayWithObjects:self.viewController, self.viewController4, self.viewController1, self.viewController3, self.viewController2, nil];

        [viewController release];
        [viewController1 release];
        [viewController2 release];
        [viewController3 release];
        [viewController4 release];

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
        self.window.rootViewController = self.rootController;
#else
        [self.window addSubview:rootController.view];
#endif
        [self.window makeKeyAndVisible];
        HUD = [[MBProgressHUD alloc] initWithView:viewController.view];
        [viewController.view addSubview:HUD];
        [HUD show:NO];

        HUD.delegate = self;

        HUD.labelText = @"Loading";


        return YES;
    

//[---CODE CLIP---]

    - (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation 
        CGFloat width = self.rootController.view.bounds.size.width*2;
        CGFloat height = self.rootController.view.bounds.size.height;
        UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, width, height)];
        UIImage *imageView = [UIImage imageNamed:@"TabBarBackground.png"];
        UIColor *kMainColor = [[UIColor alloc] initWithPatternImage:imageView];

        [v setBackgroundColor:kMainColor];
        [kMainColor release];
        [self.rootController.tabBar insertSubview:v atIndex:0];
        [imageView release];
        [v release];

    

    - (void)hudWasHidden 
        [HUD removeFromSuperview];
    

//[---CODE CLIP---]

    - (void)dealloc
    
        [_window release];
        [rootController release];
        [HUD release];
        [super dealloc];
    

    @end

问题是当我在 ios 模拟器中旋转设备时,应用程序不会旋转。任何想法将不胜感激!

更新

我还注意到启动图像也没有旋转(对于 iPad,即 iPhone 不做横向启动图像)。

JMANS 的注意事项

我覆盖了 UITabBarController:

@implementation UITabBarController (MyApp)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
    if(self.selectedIndex==4) 
        return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
    else
        return (toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

@end

【问题讨论】:

您是否检查了模拟器中的纵向方向是否被锁定(双击主页按钮并向右滑动多任务栏以从左侧显示控件。有一个方向锁定控件) @Bobj-C - 不,这不是问题,但感谢您的回答!似乎iOS模拟器无法锁定方向,因为每次单击它时,它都会显示“人像方向已解锁”。 【参考方案1】:

我会首先将它包含在您的所有视图控制器中:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    return YES;
 

另外,请确保您在 Info.plist 中支持多个方向。

【讨论】:

谢谢,原来我必须为所有的 viewControllers 返回 YES。甚至那些我不想旋转的!虽然我不知道如何让一个或两个选项卡不旋转,同时仍然允许其他选项卡旋转。 那么您是否希望标签视图始终旋转,但内容有时只旋转?或者您是否希望选项卡视图根据选择的选项卡旋转?您可以在某处(例如您的 App Delegate)设置一个变量,指定当前是否启用某些方向,然后在从各种 shouldAutorotate... 方法返回之前检查该变量。 查看我的问题的底部。 这是一个拔毛器——谢谢你的提示!我让一切正常,然后在情节提要中添加了一个新的视图控制器,突然间,它停止了旋转!您必须确保的另一件事是在 -info.plist 中正确设置了“支持的界面方向”。【参考方案2】:

标签栏控制器和视图旋转

标签栏控制器默认支持纵向,除非所有包含的视图控制器都支持这样的方向,否则不会旋转到横向。当设备方向发生变化时,标签栏控制器会查询其视图控制器数组。如果其中任何一个不支持方向,则标签栏控制器不会改变其方向。

【讨论】:

这在许多其他解决方案都失败后起作用了......谢谢!赞一个!

以上是关于iOS (iPhone/iPad) SDK - 应用程序不改变方向的主要内容,如果未能解决你的问题,请参考以下文章

同步服务和 iOS

为 iPhone SDK 3.2 准备通用应用程序

iPhone/iPad 项目 xCode

iOS 配件 SDK - 3.5mm 插孔控制

iPhone iPad 蓝牙传输

facebook ios sdk 或苹果社交框架