2017-03-06 19 views
0

我是iOS新手,並且遇到關於在fount Awesome按鈕上顯示通知計數的問題。 我的代碼是這樣的我如何顯示目標C中的UINavigation欄上的通知計數

// Notificaation圖標按鈕...

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 
    { 
     [email protected]"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

如何通知我可以展示它嗎? 在此先感謝!

回答

1

創建Bagde對視圖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; 
} 

添加徽章上的導航欄(設置你的X和Y):

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

@SiddeshMhatra我需要證明它lablnotification。 – Muju

+0

在'UILabel * bagdeLabel = [self DisplayBagde:30:10:'your notification count'];' –

+0

添加徽章數你想要的東西傳遞你的x和y座標(你希望顯示在視圖上)like ur case x = btn.width-10,y = 0和通知計數。 –

0

試試這個: https://www.cocoacontrols.com/controls/mibadgebutton

然後添加到您的視圖控制器

MIBadgeButton *btn1 = [MIBadgeButton buttonWithType:UIButtonTypeCustom]; 
 
[btn1 setFrame:CGRectMake(100, 150, 64, 30)]; 
 
[btn1 setTitle:@"test" forState:UIControlStateNormal]; 
 
[btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 
 
[btn1 setBadgeString:@"244"]; 
 
[self.view addSubview:btn1];

相關問題