2013-05-24 2 views
2

Je dois ajouter un style de bordure en pointillés pour UIView. Je ne veux pas utiliser CGRect et setDash car vous ne pouvez pas le retirer après l'avoir configuré. Je devrais être capable de basculer avec ce style de bordure. Comment je m'en occupe?Bordure en pointillés pour UIView

J'ai une méthode pour ajouter

-(void) addBorder 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetStrokeColorWithColor(context, [[UIColor blueColor] CGColor]); 

    CGContextAddRect(context, self.bounds); 
    CGContextStrokePath(context); 
} 

Je reçois l'erreur « contexte invalide 0x0 »

+0

Pouvez-vous éditer votre question pour montrer comment vous ajoutez actuellement votre frontière? –

+0

Vous pourriez obtenir une réponse d'ici Message similaire: http: //stackoverflow.com/questions/13679923/dotted-line-border-around-uiview – Raj

+0

Voir cette réponse http://stackoverflow.com/a/40512209/1757229 – sash

Répondre

0

Ajouter QuartzCore/QuartzCore.h cadre dans le projet et importer #import <QuartzCore/QuartzCore.h> dans le fichier .m.

Et puis essayer code suivant:

[view.layer setBorderWidth:5.0]; 
[view.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:@"DottedImage.png"]] CGColor]]; 

espoir que cela vous aide.

+0

Nope , lorsque je redimensionne ma vue, l'image sera mauvaise. –

1

Essayez la méthode suivante, une de mes applications a également la même fonctionnalité & Je l'ai fait en utilisant le chemin suivant.

-(void)addDashedBorder 
{ 
    //border definitions 
    CGFloat cornerRadius = 0; 
    CGFloat borderWidth = 1; 
    NSInteger dashPattern1 = 4; 
    NSInteger dashPattern2 = 4; 
    UIColor *lineColor = [UIColor blackColor]; 

    //drawing 
    CGRect frame = view.bounds; 

    CAShapeLayer *_shapeLayer = [CAShapeLayer layer]; 

    //creating a path 
    CGMutablePathRef path = CGPathCreateMutable(); 

    //drawing a border around a view 
    CGPathMoveToPoint(path, NULL, 0, frame.size.height - cornerRadius); 
    CGPathAddLineToPoint(path, NULL, 0, cornerRadius); 
    CGPathAddArc(path, NULL, cornerRadius, cornerRadius, cornerRadius, M_PI, -M_PI_2, NO); 
    CGPathAddLineToPoint(path, NULL, frame.size.width - cornerRadius, 0); 
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, cornerRadius, cornerRadius, -M_PI_2, 0, NO); 
    CGPathAddLineToPoint(path, NULL, frame.size.width, frame.size.height - cornerRadius); 
    CGPathAddArc(path, NULL, frame.size.width - cornerRadius, frame.size.height - cornerRadius, cornerRadius, 0, M_PI_2, NO); 
    CGPathAddLineToPoint(path, NULL, cornerRadius, frame.size.height); 
    CGPathAddArc(path, NULL, cornerRadius, frame.size.height - cornerRadius, cornerRadius, M_PI_2, M_PI, NO); 

    //path is set as the _shapeLayer object's path 
    _shapeLayer.path = path; 
    CGPathRelease(path); 

    _shapeLayer.backgroundColor = [[UIColor clearColor] CGColor]; 
    _shapeLayer.frame = frame; 
    _shapeLayer.masksToBounds = NO; 
    [_shapeLayer setValue:[NSNumber numberWithBool:NO] forKey:@"isCircle"]; 
    _shapeLayer.fillColor = [[UIColor clearColor] CGColor]; 
    _shapeLayer.strokeColor = [lineColor CGColor]; 
    _shapeLayer.lineWidth = borderWidth; 
    _shapeLayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:dashPattern1], [NSNumber numberWithInt:dashPattern2], nil]; 
    _shapeLayer.lineCap = kCALineCapRound; 

    //_shapeLayer is added as a sublayer of the view, the border is visible 
    [view.layer addSublayer:_shapeLayer]; 
    view.layer.cornerRadius = cornerRadius; 
} 
Questions connexes