如何计算最佳titleView宽度?

时间:2009-08-06 23:15:52

标签: iphone uinavigationcontroller uinavigationbar uibarbuttonitem uinavigationitem

我正在使用带有自定义UIView子类的导航,该子类成为我的titleView。我想确保这是完整的可用宽度。

从我的UIViewController viewWillAppear:开始,这应该是:

CGFloat width = self.width - self.navigationItem.leftBarButtonItem.width - someConstant;

(我这里没有正确的项目。)

然后,这将适应leftBarButtonItem的不同可能宽度。问题是leftBarButtonItemnil,所以leftBarButtonItem.width始终为0(好吧,无论如何都在模拟器中)。

backBarButtonItem也是零。

我应该做什么呢?

1 个答案:

答案 0 :(得分:4)

我通过执行以下操作来破解这个:

    int vcStackSize = [[self.navigationController viewControllers] count];
    WEViewController* previousController = [[self.navigationController viewControllers] objectAtIndex:vcStackSize - 2];

    NSString* previousTitle = [previousController title];

    UIFont* font = [UIFont boldSystemFontOfSize:12];


    lw = [previousTitle sizeWithFont:font].width + 26;

讨厌,但它有效

相关问题