UINavigationItem多行提示文本

时间:2012-06-28 07:49:11

标签: uinavigationcontroller prompt uinavigationitem

任何机构都可以给我解决方案,用2行显示UINavigationItem提示文字吗?

1 个答案:

答案 0 :(得分:2)

没有内置方法可以做到这一点。以下是一个解决方案似乎工作得很好,我从stackOverflow帖子UINavigationItem with prompt and activity indicator

放在一起

这是它创建的模拟器屏幕截图: iPhone screen shot of two line UINavigationItem prompt

请注意,由于文字是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或其他非法代码。