简体   繁体   English

在iOS的导航栏中添加通知标志

[英]adding notification badge to Navigation bar in iOS

I'm trying to add a badge to the navigation bar of my iOS app,more precisely to the Menu button which triggers my side menu. 我正在尝试将徽章添加到我的iOS应用程序的导航栏中,更确切地说是添加到触发我的侧菜单的“菜单”按钮。 I'm using this code 我正在使用此代码

// add badge to the menu button
if (!self.numberBadge) {
         self.numberBadge = [[MLPAccessoryBadgeEmboss alloc] initWithFrame:CGRectZero];
       }
          numberBadge.center = CGPointMake(30.0, 6);
          numberBadge.badgeMinimumSize = CGSizeMake(1.0, 1.0);
          numberBadge.backgroundColor = [UIColor redColor];
          numberBadge.shadowAlpha = 0.9;
          numberBadge.cornerRadius = 5.0f;
          numberBadge.strokeColor = [UIColor whiteColor];
          [self.navigationController.navigationBar addSubview:numberBadge];
          [numberBadge setTextWithIntegerValue:9]; 

as result, the badge gets displayed like that: 结果,徽章显示为:

徽章

now I've been trying both modifying the frame and the centre of the badge uiview but, no matter what I do, it will stick to the upper-left corner of the navigation bar. 现在,我一直在尝试同时修改框架和徽章uiview的中心,但是,不管我做什么,它都将粘贴在导航栏的左上角。 It's currently impossible for me to move it. 我目前无法移动它。

Any suggestions? 有什么建议么?

It looks like your badge is always positioned at (0, 0) in the navigation bar. 您的徽章似乎始终位于导航栏中的(0,0)。

I assume that when you assign self.numberBadge, you are assigning it to an instance variable called numberBadge, otherwise you would had to use self.numberBadge instead. 我假设当您分配self.numberBadge时,是将其分配给一个名为numberBadge的实例变量,否则您将不得不使用self.numberBadge。

I would try these things: 我会尝试这些事情:

  • creating the view with a frame not CGRectZero, but the frame where you want to place the badge. 使用不是CGRectZero的框架来创建视图,而是使用要在其上放置徽标的框架。 Sometimes views created with frame zero haven't behaved as I expected. 有时,使用零帧创建的视图无法达到我的预期。
  • try moving the badge center after the call to addSubview, just to make sure it won't mess with your frame. 尝试在调用addSubview之后移动徽标中心,以确保它不会与您的框架混淆。

If it still doesn't work, can you try adding the badge to a plain UIView and check if it works there? 如果仍然无法使用,您可以尝试将徽章添加到普通的UIView并检查它是否可以在那里工作吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM