【问题标题】:Can we add additional UILabels to UINavigation Bar besides its title?除了标题之外,我们可以向 UINavigation Bar 添加额外的 UILabel 吗?
【发布时间】:2012-01-15 15:05:34
【问题描述】:

我正在开发一个应用程序,我必须在 UINavigation Bar 上显示应用程序的状态。我将使用 UILabel 来显示状态。我想知道这是否可能?

问候, 苏米特

【问题讨论】:

    标签: ios uinavigationcontroller uilabel uinavigationbar uinavigationitem


    【解决方案1】:

    是的。要做的事情是

    • 创建要显示的视图(UIView 的子类),其中包含所有标签。
      • 如果您只想显示一个标签,则不需要第一步,因为您可以直接使用UILabel
    • initWithCustomView:myLabelsView 的帮助下将您的视图包裹在UIBarButtonItem
    • 用你的UIBarButtonItem-instance 在[myViewController.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO] 上调用setRightBarButtonItem:animated:

    例子:

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

    【讨论】:

      【解决方案2】:

      我不喜欢看到疯狂的用户界面(而且我认为您的用户也不会喜欢它——导航栏中没有那么多空间或空间可供使用),但是技术上对你的正确答案是是的,你可以

      创建一个嵌入了多个标签(或任何您想要的标签)的视图,并将您的UINavigationItemtitleView 属性设置为此。

      【讨论】:

        【解决方案3】:
        UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(30,20,200,30)];
        
        navLabel.text = @"Your Text";
        navLabel.textColor = [UIColor redColor];
        
        [self.navigationController.view addSubview:navLabel];
        

        【讨论】:

          【解决方案4】:

          你可以在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;
          }
          

          }

          【讨论】:

            猜你喜欢
            • 2015-03-08
            • 2010-11-12
            • 1970-01-01
            • 1970-01-01
            • 2014-08-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多