---
title: "ios自定义BadgeValue"
author: "修博龙泉"
date: "2012-10-23T16:14:01Z"
tags: []
categories: ["移动开发"]
slug: "ios-badgevalue"
draft: false
lastmod: "2012-10-23T16:14:01Z"
id: 1703792
url: "https://longquan.iteye.com/blog/1703792"
---

```java

//
//  ViewController.m
//  Badge
//
//  Created by Bo Xiu on 12-10-23.
//  Copyright (c) 2012年 Bo Xiu. All rights reserved.
//

#import "ViewController.h"
#import "JSBadgeView.h"
#import <QuartzCore/QuartzCore.h>
#define kViewBackgroundColor [uicolor colorwithred:0.357 green:0.757 blue:0.357 alpha:1]
#define kSquareSideLength 64.0f
#define kSquareCornerRadius 10.0f
#define kMarginBetweenSquares 10.0f
#define kSquareColor [uicolor colorwithred:0.004 green:0.349 blue:0.616 alpha:1]
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewdidload];
	// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewDidAppear:(BOOL)animated{
    
    [self showbadgevalue:@"10"];
}

- (void)showBadgeValue:(NSString *)strBadgeValue
{
    UIScrollView *scrollView = [[uiscrollview alloc] initWithFrame:self.view.bounds];
    [self.view addsubview:scrollview];
    
    UIView *rectangle = [[uiview alloc] initWithFrame:CGRectMake(0,0,100,100)];
    rectangle.backgroundColor = kSquareColor;
    rectangle.layer.cornerRadius = kSquareCornerRadius;
    rectangle.layer.shadowColor = [uicolor blackcolor].CGColor;
    rectangle.layer.shadowOffset = CGSizeMake(0.0f, 3.0f);
    rectangle.layer.shadowOpacity = 0.4;
    rectangle.layer.shadowRadius = 1.0;
    
    UIImageView *imageView = [[uiimageview alloc]initWithFrame:CGRectMake(100, 300,100, 100)];
    [imageview setimage:[uiimage imagenamed:@"background.png"]];
    
    
    JSBadgeView *badgeView = [[jsbadgeview alloc] initWithParentView:rectangle alignment:JSBadgeViewAlignmentTopRight];
    badgeView.badgeText = [nsstring stringwithformat:@"%d",10];
    
    JSBadgeView *badgeView1 = [[jsbadgeview alloc] initWithParentView:imageView alignment:JSBadgeViewAlignmentCenterLeft];
    badgeView1.badgeText = [nsstring stringwithformat:@"%d",10];
    
    [scrollview addsubview:imageview];
    [scrollview addsubview:rectangle];
    [scrollview sendsubviewtoback:rectangle];
   
       
}

- (void)removeBadgeValue
{
    for (UIView *subview in self.view.subviews) {
        NSString *strClassName = [nsstring stringwithutf8string:object_getclassname(subview)];
        if ([strclassname isequaltostring:@"uitabbarbuttonbadge"] ||
            [strclassname isequaltostring:@"_uibadgeview"]) {
            [subview removefromsuperview];
            break;
        }
    }
}

- (void)viewDidUnload
{
    [super viewdidunload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if ([[uidevice currentdevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

@end


```