如何计算最佳titleView宽度?

Ste*_*her 5 iphone uinavigationbar uinavigationcontroller uibarbuttonitem uinavigationitem

我使用导航与自定义UIView子类成为我的titleView.我想确保这是完整的可用宽度.

从逻辑上讲,从我UIViewControllerviewWillAppear:,这应该是:

CGFloat width = self.width - self.navigationItem.leftBarButtonItem.width - someConstant;
Run Code Online (Sandbox Code Playgroud)

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

这将适应不同的可能宽度leftBarButtonItem.美中不足的是,leftBarButtonItemnil,所以leftBarButtonItem.width始终为0(好,在模拟器反正).

backBarButtonItem 也没有.

我应该做什么呢?

Sam*_*Sam 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;
Run Code Online (Sandbox Code Playgroud)

恶心但有效