更改视图控制器时,UINavigationBar标题会被裁剪

6
当我从iOS应用程序的起始屏幕(推送设置屏幕)推出新的tableViewController时,UINavigationController中的标题会被裁剪,直到动画完成为止。这是动画中的NavigationBar,在动画即将完成时,它看起来像这样:(插入图片1)。片刻之后,标题正确地更改为“设置”。 这不是什么大问题,但你可以想象一下,这会让一个稍微有点强迫症倾向的程序员感到多么困扰! :) 这是在tableViewController中设置标题的代码,没有什么特别之处:
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.title = @"Settings";
        // Hide tabBar when pushed so you cannot switch from the Settings
        self.hidesBottomBarWhenPushed = YES;
        self.tableView.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bg.png"]];
    }
    return self;
}

2
我也有这个烦人的问题。在我的情况下,我正在为导航标题使用自定义字体,并使用setTitleTextAttributes:forState:进行设置,如果我不更改它,则不会发生这种情况。此外,这个“问题”似乎在iOS6中已经解决了。我很想找到一个解决方法,这就是为什么我开始了一项悬赏以引起一些注意... - Javier Soto
你能把bg.png放在Dropbox或类似的地方吗? - David H
你是否对以下任何一个进行了子类化:UINavigationBarUINavigationItemUINavigationController - Ander
是的,这个问题在iOS 6中已经解决了。答案也适用于iOS 5,在我的情况下至少如此。我没有子类化任何东西,我只是使用UIAppearance来全局改变UINavigationBars的外观。 - nikolovski
3个回答

1
我回答有点晚了,但我在iOS 5上找到了问题所在。当您在UINavigationBar上使用UIAppearance代理时,似乎需要显式设置字体大小,而不是使用0.0来根据方向自动设置。
我通过子类化UINavigationController并放入以下代码来解决这个问题:
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    // You should include a conditional here to check for iOS 5, so iOS 6 doesn't have to do any additional work
    self.navigationBar.titleTextAttributes = @{
        UITextAttributeFont:[UIFont boldSystemFontOfSize:UIInterfaceOrientationIsPortrait(self.interfaceOrientation) || IS_IPAD ? 20.0f : 16.0f],
        UITextAttributeTextColor:[UIColor whiteColor],
        UITextAttributeTextShadowColor:[UIColor colorWithWhite:0.0f alpha:0.5f],
        UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0.0f, -1.0f)]
    };
}

是的,将字体大小设置为硬编码值解决了问题。这是之前的代码: [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Akzidenz-Grotesk BQ" size:0.0], UITextAttributeFont, nil]];我只是将字体大小改成了固定值:[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Akzidenz-Grotesk BQ" size:22.0], UITextAttributeFont, nil]];现在完美解决了。谢谢! - nikolovski

0

尝试

- (void)viewWillAppear:(BOOL)animated {

    self.title = @"Settings";
}

1
顺便提一下,根据文档,"[如果你重写此方法,你必须在实现中的某个时刻调用super。]" - Rob

0
尝试设置
self.navigationItem.title = self.title;

在viewWillAppear方法中


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接