回答

3

我不喜歡看瘋狂的用戶界面(我不認爲你的用戶所想,要麼 - 沒有那麼多房地產或工作空間在導航欄),但對你的技術上正確的答案在這裏是是你可以

創建多個標籤視圖(或嵌入任何你想要的),在它和titleView屬性設爲您的UINavigationItem的到那。

7

是的。要做的事情是

  • 創建您想要顯示的視圖(UIView的子類),其中包含所有標籤。
    • ,如果你只是想表明一個單一的標籤比則不需要此第一步,因爲你可以使用UILabel直接
  • 包裹在UIBarButtonItem您的視圖與initWithCustomView:myLabelsView
  • 呼叫setRightBarButtonItem:animated:的幫助你UIBarButtonItem -instance上[myViewController.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO]

例子:

-(void) viewDidLoad { 
    .... 
    UIView *myNiceLabelView = [UILabel ...]; 
    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myNiceLabelView]; 
    [self.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO]; 
    .... 
} 
0

可以在viewDidLoad中添加此功能

- (void) setRightNavView 

{

//替換titleview的

CGRect headerTitleSubtitleFrame = CGRectMake(260, 0, 60, 44); 
UIView* _headerTitleSubtitleView = [[UILabel alloc] initWithFrame:headerTitleSubtitleFrame]; 
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor]; 
_headerTitleSubtitleView.autoresizesSubviews = YES; 


CGRect titleFrame = CGRectMake(0, 2, 60, 20); 
UILabel *titleView = [[UILabel alloc] initWithFrame:titleFrame]; 
titleView.backgroundColor = [UIColor clearColor]; 
titleView.font = [UIFont boldSystemFontOfSize:12]; 
titleView.textAlignment = NSTextAlignmentCenter; 
titleView.textColor = [UIColor blackColor]; 
titleView.text = @"Balance"; 
titleView.adjustsFontSizeToFitWidth = YES; 
[_headerTitleSubtitleView addSubview:titleView]; 

CGRect subtitleFrame = CGRectMake(0, 22, 60, 22); 
UILabel *subtitleView = [[UILabel alloc] initWithFrame:subtitleFrame]; 
subtitleView.backgroundColor = [UIColor clearColor]; 
subtitleView.font = [UIFont boldSystemFontOfSize:15]; 
subtitleView.textAlignment = NSTextAlignmentCenter; 
subtitleView.textColor = [UIColor redColor]; 
subtitleView.text = @"sdfs"; 
subtitleView.adjustsFontSizeToFitWidth = YES; 
[_headerTitleSubtitleView addSubview:subtitleView]; 

_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | 
              UIViewAutoresizingFlexibleRightMargin | 
              UIViewAutoresizingFlexibleTopMargin | 
              UIViewAutoresizingFlexibleBottomMargin); 

[self.navigationController.navigationBar addSubview:_headerTitleSubtitleView]; 

}

然後,你可以在任何地方設置此功能你想要:

-(void) setBalanceTop:(NSString*)balance{ 
//NSArray *arrView = self.navigationController.navigationBar.subviews; 
//NSLog(@"So view con: %d", (int)arrView.count); 
//NSLog(@"Desc: %@", arrView.description); 

//指數在這裏取決於你Naviga控制器有多少子視圖

if([self.navigationController.navigationBar.subviews objectAtIndex:2] != nil){ 
    UIView* balanceView = [self.navigationController.navigationBar.subviews objectAtIndex:2]; 
    UILabel* titleView = [balanceView.subviews objectAtIndex:1]; 
    NSLog(@"Type class: %@", [titleView description]); 
    if((titleView != nil) && ([titleView isKindOfClass:[UILabel class]])) 
     titleView.text = balance; 
} 

}

2
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(30,20,200,30)]; 

navLabel.text = @"Your Text"; 
navLabel.textColor = [UIColor redColor]; 

[self.navigationController.view addSubview:navLabel]; 
相關問題