1

任何机构都可以为我提供两行显示 UINavigationItem 提示文本的解决方案吗?

4

1 回答 1

2

没有内置的方法可以做到这一点。下面是我从 stackOverflow 帖子UINavigationItem 与提示和活动指示器放在一起的一个似乎工作得很好的解决方法

这是它创建的模拟器屏幕截图: 两行 UINavigationItem 提示的 iPhone 屏幕截图

请注意,由于文本是 a UILabel,因此您也可以修改其颜色、字体或其他任何内容。

// I have this code in viewDidLoad
UIView                      *viewContainingPrompt;
UIBarButtonItem             *promptButtonItem;

// Configuring the prompt title of the navigation bar so it is present but empty
[self.navigationItem setPrompt: @""];

// We will create a UIBarButtonItem that has a custom view (viewContainingPrompt).
// A subview of viewContainingPrompt will be a UILabel (headerLabel)
// We need to have this "intermediate" view to position the label at the right position 
// (the UIBarButtonItem ignores the origin and height of its custom view)
viewContainingPrompt = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 0, 85)];
viewContainingPrompt.autoresizingMask = UIViewAutoresizingFlexibleWidth;

// Choose a width that puts 10 points on either end...
CGFloat labelWidth = self.navigationController.navigationBar.bounds.size.width - 20.0;
// Note that the '-60' below is determined by the width of the back button
// If someone can figure out how to determine this width at runtime this code
// would be much more robust.
UILabel *headerLabel = [[UILabel alloc] initWithFrame: CGRectMake(-60,-8,labelWidth,36)];
headerLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
headerLabel.text = @"A quite long prompt string that will wrap to a second line to demonstrate multiline prompt.";
headerLabel.font = [UIFont systemFontOfSize: 14];
headerLabel.numberOfLines = 0; // Zero gives as many lines as will fit, could be 2
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.textColor = [UIColor colorWithRed: .1 green: .1 blue: .2 alpha: 0.8f];
headerLabel.shadowColor = [UIColor colorWithRed: 1 green: 1 blue: 1 alpha: 0.5f];
headerLabel.shadowOffset = CGSizeMake( 0, 1 );
headerLabel.textAlignment = UITextAlignmentCenter;
[viewContainingPrompt addSubview: headerLabel];
//[headerLabel release]; // Uncomment if not using ARC

promptButtonItem = [[UIBarButtonItem alloc] initWithCustomView: viewContainingPrompt];
self.navigationItem.leftBarButtonItem = promptButtonItem;
self.navigationItem.leftItemsSupplementBackButton = YES;
//[viewContainingPrompt release]; // Uncomment if not using ARC
//[promptButtonItem release]; // Uncomment if not using ARC

我将不胜感激任何人就如何在执行期间计算后退按钮的宽度提供反馈,以便不必对宽度进行硬编码。

因为它是我不认为包含任何私有 API 或其他非法代码。

于 2012-08-22T12:51:33.757 回答