2012-06-28 71 views

回答

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或其他非法代碼包含。