iOS 8后退按钮文本问题

时间:2014-12-08 12:04:41

标签: iphone uinavigationcontroller ios8

以下是用于后退按钮文本的代码

UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape!           
[backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:parent.title forState:UIControlStateNormal];

if (backButton.frame.size.width >= 140.0f)
            {
                [backButton setFrame:CGRectMake(0.0f, 0.0f, 140.0f, 30.0f)];
            }
backNaviButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];

以上代码适用于iOS 7及以下版本

以下是比较屏幕截图

iOS 7

enter image description here

iOS 8 enter image description here

1 个答案:

答案 0 :(得分:2)

测试字符串长度而不是按钮宽度

UIButton* backButton = [UIButton buttonWithType:101]; // left-pointing shape!           
[backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];

if (parent.title.length>maxStringLength) {

    [backButton setTitle:[NSString stringWithFormat:@"%@...",[parent.title substringToIndex:maxStringLength]] forState:UIControlStateNormal];
} else {
    [backButton setTitle:parent.title forState:UIControlStateNormal];
}