1

In order to show a red point on the top right corner of a button,

Does any system method or third party could be used?

As a freshman , please tell me the detail code, thank you...

Ilesh P
  • 3,940
  • 1
  • 24
  • 49
  • 1st thing that is called `Badge`, 2nd Where you want to show it? – Toseef Khilji Jul 30 '14 at 08:54
  • The solution depends on how it should look and behave like. There's no system method to achieve this using regular `UIButton`. Simplest way to workaround that is to add `UILabel` as subview. – Andriy Jul 30 '14 at 09:03
  • hi @user3871190 check it here http://stackoverflow.com/questions/4722669/how-can-i-add-a-badge-to-a-standard-uibutton – Ilesh P Jul 30 '14 at 09:12

2 Answers2

1

With a quick search I would share the following github project too: https://github.com/szemian/DDBadgeViewCell

Using eg. the standard table management source code you can utilise DDBadgeViewCell like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    DDBadgeViewCell *cell = (DDBadgeViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[DDBadgeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell.
    cell.summary = @"Summary";
    cell.detail = @"Detail text goes here";
    cell.badgeText = [NSString stringWithFormat:@"Row %d", indexPath.row];
    cell.badgeColor = [UIColor orangeColor];

    return cell;
}

Please note that you set the badge information in cell.badgeText.

Source code from the example project.

nzs
  • 3,252
  • 17
  • 22
0

for UIButton there is no system methods

but you can find custom classes on Github: https://github.com/search?l=Objective-C&q=Badge&ref=searchresults&type=Repositories

for example: https://github.com/ckteebe/CustomBadge

UPDATE:

// Create simple Badge
CustomBadge *customBadge1 = [CustomBadge customBadgeWithString:@"2" 
                                                   withStringColor:[UIColor whiteColor] 
                                                    withInsetColor:[UIColor redColor] 
                                                    withBadgeFrame:YES 
                                               withBadgeFrameColor:[UIColor whiteColor] 
                                                         withScale:1.0
                                                       withShining:YES];        
// Set Position of Badge 1
[customBadge1 setFrame:CGRectMake(self.view.frame.size.width/2-customBadge1.frame.size.width/2+customBadge2.frame.size.width/2, 110, customBadge1.frame.size.width, customBadge1.frame.size.height)];
// Add Badges to View
[self.view addSubview:customBadge2];

Code has been taken from CustomBadge example

zhukov.ever
  • 557
  • 3
  • 10