导航栏颜色和标题

发布于 2024-11-28 14:45:59 字数 2391 浏览 0 评论 0原文

我有很多 .xib 文件,我尝试以编程方式重新创建它们。

唯一的问题是,我无法再自定义导航栏。

De versions 函数由具有多个栏按钮项的另一个视图调用,因此该代码对于其他函数是通用的。

- (void)versions
{
    VersionsViewController *verController = [[VersionsViewController alloc] initWithDocument:document];
    [verController setDelegate:self];

    [self loadPopupView:verController];

    [verController release];
}

- (void)loadPopupView:(UIViewController *)viewController
{
    if (popOverController != nil && [popOverController isPopoverVisible]) 
    {
        [popOverController dismissPopoverAnimated:YES];
    }

    if(![popOverController isPopoverVisible] || ![popOverController.contentViewController isKindOfClass:[viewController class]])
    {
        popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
        popOverController.popoverContentSize = CGSizeMake(320, 500);

        UIBarButtonItem *buttonLocation;

        if([viewController isKindOfClass:[CommentaryViewController class]])
            buttonLocation = commentaryButton;
        else if([viewController isKindOfClass:[PropertiesViewController class]])
            buttonLocation = propertiesButton;
        else
            buttonLocation = versionsButton;

        [popOverController presentPopoverFromBarButtonItem:buttonLocation permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}

VersionsViewController.m

- (id)initWithDocument:(Document *)doc
{
    self = [super init];

    if(self)
    {
        self.document = doc;
        self.title = @"other title"; //does not work either

        //I just tried everything I could think of :P
        self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
        self.navigationItem.titleView.backgroundColor = [UIColor redColor];
        self.navigationController.navigationItem.titleView.backgroundColor = [UIColor greenColor];
        self.navigationController.tabBarController.tabBar.backgroundColor = [UIColor blueColor];
        self.navigationController.navigationBar.backgroundColor = [UIColor purpleColor];
    }

    return self;
}

有人能看到我做错了什么吗?

编辑:

来自 self.title 和 self.navigationController.title 的 NSLOG 都是“null”

当我创建 navigationController 添加视图并将 navigationController 添加到弹出窗口时,我得到 2 个栏,然后我可以设置 navigationController 的标题,但仍然不行颜色。

I had a lot of .xib files and I try to recreate them programmatically.

The only problem is, is that I can't customize my navigationbar any more.

De versions function is called by another view with multiple bar button items, so the code is generic to other functions to.

- (void)versions
{
    VersionsViewController *verController = [[VersionsViewController alloc] initWithDocument:document];
    [verController setDelegate:self];

    [self loadPopupView:verController];

    [verController release];
}

- (void)loadPopupView:(UIViewController *)viewController
{
    if (popOverController != nil && [popOverController isPopoverVisible]) 
    {
        [popOverController dismissPopoverAnimated:YES];
    }

    if(![popOverController isPopoverVisible] || ![popOverController.contentViewController isKindOfClass:[viewController class]])
    {
        popOverController = [[UIPopoverController alloc] initWithContentViewController:viewController];
        popOverController.popoverContentSize = CGSizeMake(320, 500);

        UIBarButtonItem *buttonLocation;

        if([viewController isKindOfClass:[CommentaryViewController class]])
            buttonLocation = commentaryButton;
        else if([viewController isKindOfClass:[PropertiesViewController class]])
            buttonLocation = propertiesButton;
        else
            buttonLocation = versionsButton;

        [popOverController presentPopoverFromBarButtonItem:buttonLocation permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}

VersionsViewController.m

- (id)initWithDocument:(Document *)doc
{
    self = [super init];

    if(self)
    {
        self.document = doc;
        self.title = @"other title"; //does not work either

        //I just tried everything I could think of :P
        self.navigationController.navigationBar.tintColor = [UIColor orangeColor];
        self.navigationItem.titleView.backgroundColor = [UIColor redColor];
        self.navigationController.navigationItem.titleView.backgroundColor = [UIColor greenColor];
        self.navigationController.tabBarController.tabBar.backgroundColor = [UIColor blueColor];
        self.navigationController.navigationBar.backgroundColor = [UIColor purpleColor];
    }

    return self;
}

Can someone see what I did wrong?

EDIT:

NSLOG from self.title and self.navigationController.title are both 'null'

When I create a navigationController add the view and add the navigationController to the popup i get 2 bars and then I can set the title of the navigationController but still not the color.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这样的小城市 2024-12-05 14:45:59

只是好奇:如果您有工作 nib 文件,为什么要以编程方式重新创建它们?

initWithDocument: 中,您尚未分配 nvaigationController。您需要手动构建其中一个,并在其中安装 VersionViewControllernavigationController 将在您完成此操作后设置)。自动完成所有这些工作是我们使用 nib 文件的原因之一。

编辑任何时候“没有发生任何事情”,请检查您正在消息传递的对象是否为nil。我确信当您到达时 navigationController 仍然是 nil 。您还应该NSLog您关心的对象并查看它们的地址。您可能不小心创建了多个视图或多个导航控制器。当您尝试手动构建这些东西时,这是很常见的做法。在大多数情况下,笔尖是正确的解决方案。

Just curious: if you have working nib files, why are your recreating them programmatically?

In initWithDocument:, you haven't assigned a nvaigationController yet. You'll need to build one of those by hand, and install VersionViewController in it (navigationController will be set after you've done that). Having all of this work automatically is one of the reasons we use nib files.

EDIT Anytime "nothing happens" check that the object you're messaging is not nil. I'm certain navigationController is still nil when you get to it. You should also NSLog the objects you care about and look at their addresses. You probably are accidentally creating multiple views or multiple navigation controllers. This is very common to do when you try to build this stuff by hand. Nibs are the correct solution in most cases.

嗼ふ静 2024-12-05 14:45:59
- (void)versions
{
    VersionsViewController *verController = [[VersionsViewController alloc] initWithDocument:document];
    [verController setDelegate:self];
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:verController];
    [self loadPopupView:navC];
    [verController release];
    [navC release];
}
- (void)versions
{
    VersionsViewController *verController = [[VersionsViewController alloc] initWithDocument:document];
    [verController setDelegate:self];
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:verController];
    [self loadPopupView:navC];
    [verController release];
    [navC release];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文