我正在一個應用程序中,我必須在UINavigation Bar上顯示應用程序的狀態。我將使用UILabel來顯示狀態。我想知道這是否可能?除了標題外,我們還可以向UINavigation Bar添加其他UILabel嗎?
問候, 薩米特
我正在一個應用程序中,我必須在UINavigation Bar上顯示應用程序的狀態。我將使用UILabel來顯示狀態。我想知道這是否可能?除了標題外,我們還可以向UINavigation Bar添加其他UILabel嗎?
問候, 薩米特
我不喜歡看瘋狂的用戶界面(我不認爲你的用戶所想,要麼 - 沒有那麼多房地產或工作空間在導航欄),但對你的技術上正確的答案在這裏是是你可以:
創建多個標籤視圖(或嵌入任何你想要的),在它和titleView
屬性設爲您的UINavigationItem
的到那。
是的。要做的事情是
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];
....
}
可以在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;
}
}
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(30,20,200,30)];
navLabel.text = @"Your Text";
navLabel.textColor = [UIColor redColor];
[self.navigationController.view addSubview:navLabel];