gpt4 book ai didi

ios - 如何在 Objective C 中的 UI 导航栏上显示通知计数

转载 作者:行者123 更新时间:2023-11-29 00:27:42 25 4
gpt4 key购买 nike

我是 iOS 的新手,我遇到了关于在 Awesome 字体按钮上显示通知计数的问题。我的代码是这样的

//通知图标按钮...

UILabel *lablnotification =[[UILabel alloc] init];
// lab.font = [UIFont fontWithName:@"FontAwesome" size:8];
// lab.textColor = [UIColor whiteColor];
lablnotification.text = [NSString awesomeIcon:FaBell];

UIImage *listImage4 = [UIImage imageNamed:@"notification.png"];
UIButton *listButton4 = [UIButton buttonWithType:UIButtonTypeCustom];
listButton4.backgroundColor=[UIColor whiteColor];
[[listButton4 layer] setBorderWidth:0.5f];
listButton4.layer.borderColor =[[UIColor blackColor] CGColor];
listButton4.layer.cornerRadius = managementbtn.bounds.size.width / 3.4;// this value vary as per your desire
listButton4.clipsToBounds = YES;

UIFont *fontnotification = [UIFont fontWithName:@"FontAwesome" size:15.0];
UIColor *colornotification = [UIColor blueColor];

NSDictionary *attrsDictionarynotification = [NSDictionary dictionaryWithObjectsAndKeys:fontnotification,NSFontAttributeName,colornotification,NSForegroundColorAttributeName, nil];
// [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,color,NSForegroundColorAttributeName, nil];

NSAttributedString *attributedStrnotification = [[NSAttributedString alloc] initWithString:lablnotification.text attributes:attrsDictionarynotification];



// get the image size and apply it to the button frame
CGRect listButton4Frame = listButton4.frame;
listButton4Frame.size = listImage4.size;
listButton4.frame = listButton4Frame;

[listButton4 setAttributedTitle:attributedStrnotification forState:UIControlStateNormal];
[listButton4 addTarget:self
action:@selector(ActualNotificationClick:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton4 =
[[UIBarButtonItem alloc] initWithCustomView:listButton4];

方法

-(void)ActualNotificationClick:(id)sender
{
if([CheckStringVersion isEqualToString:@"1"])
{
NotificationScreen *screen =[[NotificationScreen alloc] initWithNibName:@"NotificationScreen" bundle:nil];
[self.navigationController pushViewController:screen animated:YES];
}
else
{
CheckStringPopup=@"3";
UIAlertView *alertPOP =[[UIAlertView alloc] initWithTitle:@"No Update Require" message:@"App is up to date." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertPOP show];
}
}

我需要这样显示通知

enter image description here

如何显示?提前致谢!

最佳答案

创建 Bagde 以在 View 上显示 x 和 y 可调:

-(UILabel*)DisplayBagde:(CGFloat)x :(CGFloat)y : (NSString*)val{
UILabel *lbl = [[UILabel alloc] init];
[lbl setFont:[UIFont systemFontOfSize:10]];
lbl = [[UILabel alloc]initWithFrame:CGRectMake(x, y,30,30)];//strikeWidth+10,strikeWidth+10)];
lbl.textColor = [UIColor whiteColor];
lbl.layer.cornerRadius = 15.0f;
lbl.textAlignment = NSTextAlignmentCenter;
lbl.backgroundColor = [UIColor redColor];
lbl.clipsToBounds = YES;
lbl.text = val;
return lbl;
}

在导航栏上添加角标(Badge)(设置你的 x 和 y):

UILabel *bagdeLabel = [self DisplayBagde:30 :10 :@"3"];  //Badge count = 3
[self.navigationController.navigationBar addSubview:bagdeLabel];

关于ios - 如何在 Objective C 中的 UI 导航栏上显示通知计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42623334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com