Можем ли мы добавить дополнительные UILabels в UINavigation Bar помимо его названия? - PullRequest
4 голосов
/ 15 января 2012

Я работаю над одним приложением, в котором мне нужно отображать статус приложения на панели навигации UIN. Я собираюсь использовать UILabel для отображения статуса. Мне было интересно, возможно ли это или нет?

С уважением, Sumit

Ответы [ 4 ]

7 голосов
/ 15 января 2012

Да. Что нужно сделать

  • создайте свой вид, который вы хотите отобразить (подкласс UIView), который содержит все ваши метки.
    • если вы просто хотите показать одну метку, тогда этот первый шаг не нужен, потому что вы можете использовать UILabel напрямую
  • обернуть ваш взгляд в UIBarButtonItem с помощью initWithCustomView:myLabelsView
  • вызов setRightBarButtonItem:animated: с вашим UIBarButtonItem экземпляром на [myViewController.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO]
* +1025 * Пример:
-(void) viewDidLoad {
    ....
    UIView *myNiceLabelView = [UILabel ...];
    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myNiceLabelView];
    [self.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO];
    ....
}
3 голосов
/ 15 января 2012

Мне не нравится видеть сумасшедшие пользовательские интерфейсы (и я не думаю, что вашим пользователям это тоже понравится - на панели навигации не так много недвижимости или пространства), нотехнически правильный ответ для вас здесь: да, вы можете :

Создать представление с несколькими встроенными в него метками (или как хотите) и установить UINavigationItem titleView собственность к этому.

2 голосов
/ 16 декабря 2015
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(30,20,200,30)];

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

[self.navigationController.view addSubview:navLabel];
0 голосов
/ 21 октября 2015

Вы можете добавить эту функцию в 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 Controller субвидов

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;
}

}

...