除了标题之外,我们可以向 UINavigation Bar 添加其他 UILabels 吗?

发布于 2024-12-26 18:28:18 字数 99 浏览 1 评论 0原文

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

问候, 苏米特

I am working on one app in which I have to display app's status on the UINavigation Bar. I am going to use UILabel to display the status. I was wondering whether it is possible or not ?

Regards,
Sumit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风月客 2025-01-02 18:28:18

是的。要做的事情是

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

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

Yes. The things to do are

  • create your view you want to display (subclass of UIView) which contains all your labels.
    • if you just want to show one single label than this first step is not needed because you can use the UILabel directly
  • wrap your view in an UIBarButtonItem with the help of initWithCustomView:myLabelsView
  • call setRightBarButtonItem:animated: with your UIBarButtonItem-instance on [myViewController.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO]

Example:

-(void) viewDidLoad {
    ....
    UIView *myNiceLabelView = [UILabel ...];
    UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:myNiceLabelView];
    [self.navigationController.navigationItem setRightBarButtonItem:myBarButtonItem animated:NO];
    ....
}
滿滿的愛 2025-01-02 18:28:18

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

创建一个嵌入多个标签(或任何你想要的标签)的视图,并设置你的 UINavigationItem< code>titleView 属性。

I don't like to see crazy user interfaces (and I don't think your users would like it either -- there isn't all that much real estate or space to work with in the navigation bar), but the technically correct answer for you here is yes you can:

Create a view with multiple labels (or whatever you want) embedded in it and set your UINavigationItem's titleView property to that.

看透却不说透 2025-01-02 18:28:18
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(30,20,200,30)];

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

[self.navigationController.view addSubview:navLabel];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(30,20,200,30)];

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

[self.navigationController.view addSubview:navLabel];
阳光下的泡沫是彩色的 2025-01-02 18:28:18

中添加这个函数

- (void) setRightNavView

你可以在 ViewDidLoad {

// Replace 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);

//Index at here取决于你的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;
}

}

You can add this function in ViewDidLoad

- (void) setRightNavView

{

// Replace 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];

}

then, you can set this function at any place you want:

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

//Index at here depends on your Naviga Controller has how many subviews

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文