簡體   English   中英

iOS(iPhone / iPad)SDK-應用程序未更改方向

[英]iOS (iPhone/iPad) SDK - Application not changing orientation

在我的自定義標簽欄應用程序中,即使強制旋轉狀態欄,方向也似乎從未改變。 這是我在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 Simulator中旋轉設備時,應用程序不會旋轉。 任何想法將不勝感激!

更新

我還注意到啟動圖像也沒有旋轉(對於iPad,-iPhone不做橫向啟動圖像)。

JMAN的注意事項

我覆蓋了UITabBarController:

@implementation UITabBarController (MyApp)

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

我首先將其包含在所有視圖控制器中:

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

另外,請確保您在Info.plist中支持多種方向。

標簽欄控制器和視圖旋轉

標簽欄控制器默認情況下支持縱向,除非包含的所有視圖控制器均支持橫向,否則它們不會旋轉為橫向。 當設備方向發生變化時,選項卡欄控制器將查詢其視圖控制器數組。 如果其中任何一個都不支持方向,則標簽欄控制器不會更改其方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM