【问题标题】:UINavigationController + UITabBar. Wrong vertical positioning in viewDidLoadUINavigationController + UITabBar。 viewDidLoad 中的垂直定位错误
【发布时间】:2012-11-01 13:10:15
【问题描述】:

使用 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

谢谢

【问题讨论】:

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


    【解决方案1】:

    你做错了。

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

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

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

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

    【讨论】:

    • 感谢您的回复。但我只想要一个主导航栏,这可以使用 Storyboards 来完成,所以我想这也可以用 Nibs 来完成。我也没有发现删除标签栏的问题
    • 我用你可以看到的 Storyboards 创建了一个示例项目。我想在没有故事板的情况下拥有相同的功能freefilehosting.net/atestsb
    • 不幸的是,该主机被我的 isp 阻止了。你能把它上传到其他地方或者创建一个 github 存储库吗?
    猜你喜欢
    • 2011-07-09
    • 1970-01-01
    • 2017-08-22
    • 2012-06-22
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    相关资源
    最近更新 更多