UINavigationController + UITabBar。 viewDidLoad中的垂直定位错误

时间:2012-11-01 13:10:15

标签: iphone objective-c uinavigationcontroller uitabbar xcode4.5

使用XCode 4.5& iOS6的

我创建了一个带有UITabBar(NIB)的UINavigationController,并且第一次启动的选项卡垂直定位不正确。当您单击第二个选项卡并再次单击第一个选项卡时,垂直定位就可以了。

那么......第一次运行完成后,如何正确定位第一个标签?

看错了定位:

http://img231.imageshack.us/img231/2159/badbf.png

我的代码:

AppDelegate.h

@interface bib_AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *mainControllercode;
@property (strong, nonatomic) UITabBarController *tabBarController;
AppDelegate.m中的

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // change defaul selected icon tabbar color to orange
    [[UITabBar appearance] setSelectedImageTintColor:[UIColor orangeColor]];

    // Override point for customization after application launch.
    UIViewController *viewController1 = [[agendaViewController alloc] initWithNibName:@"agendaViewController" bundle:nil];
    UIViewController *viewController2 = [[messagesViewController alloc] initWithNibName:@"messagesViewController" bundle:nil];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[viewController1, viewController2];

    self.mainControllercode = [[UINavigationController alloc] initWithRootViewController:self.tabBarController];
    self.window.rootViewController = self.mainControllercode;

    [self.window makeKeyAndVisible];
    return YES;
}

agendaViewController.h

#import <UIKit/UIKit.h>

@interface agendaViewController : UIViewController
@end

agendaViewController.m

#import "agendaViewController.h"

@interface agendaViewController ()

@end

@implementation agendaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    self.title = NSLocalizedString(@"Agenda", @"Agenda");
    self.tabBarItem.image = [UIImage imageNamed:@"83-calendar"];
}
return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

编辑1:

我创建了一个可以看到Storyboard的示例项目。我希望在没有Storyboard的情况下拥有相同的功能,请在此处下载:

http://www.freefilehosting.net/atestsb

由于

1 个答案:

答案 0 :(得分:1)

你这是错误的做法。

UITabBarController应该有一个UINavigationControllers集合,然后将其根控制器设置为主Nib。然后每个选项卡处理自己的导航堆栈。

您当前正在将UITabBarController放在UINavigationController的根目录中。当您在导航堆栈中移动时,这将导致问题并移除标签栏。

查看此链接以获取更多详细信息,以便以编程方式处理它:

http://www.xdracco.net/howto-implement-uinavigationcontroller-uitabbarcontroller-programmatically/