使用 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;
}
议程视图控制器.h
#import <UIKit/UIKit.h>
@interface agendaViewController : UIViewController
@end
议程视图控制器.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:
我用你可以看到的 Storyboards 创建了一个示例项目。我想在没有 Storyboard 的情况下拥有相同的功能,请在此处下载:
http://www.freefilehosting.net/atestsb
谢谢