Comment faire un UIView avec des coins arrondis optionnels et une bordure?

UIView rayon de coin à un UIView c'est-à-dire UIRectCornerTopLeft et UIRectCornerTopRight . Quand je l'applique, la bordure est partie dans les coins. Comment éviter cela?

Voici comment j'applique la bordure à UIView :

  [self.middleView addRoundedCorners:UIRectCornerTopLeft|UIRectCornerTopRight withRadii:CGSizeMake(4, 4)]; self.middleView.layer.borderWidth = 0.5f; self.middleView.layer.borderColor = [[UIColor colorWith8BitRed:0 green:0 blue:0 alpha:0.25] 

Et c'est une catégorie que j'utilise pour appliquer des coins arrondis optionnels:

  #import "UIView+Roundify.h" @implementation UIView (Roundify) - (void)addRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii { CALayer *tMaskLayer = [self maskForRoundedCorners:corners withRadii:radii]; self.layer.mask = tMaskLayer; } - (CALayer*)maskForRoundedCorners:(UIRectCorner)corners withRadii:(CGSize)radii { CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = self.bounds; UIBezierPath *roundedPath = [UIBezierPath bezierPathWithRoundedRect: maskLayer.bounds byRoundingCorners:corners cornerRadii:radii]; maskLayer.fillColor = [[UIColor whiteColor] CGColor]; maskLayer.backgroundColor = [[UIColor clearColor] CGColor]; maskLayer.path = [roundedPath CGPath]; return maskLayer; } 

Essayez ci-dessous le code ça marche

Votre vue que vous souhaitez arrondir TopLeft et TopRight

  UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(50, 100, 100, 100)]; [view1 setBackgroundColor:[UIColor grayColor]]; [self.view addSubview:view1]; 

Définir le coin comme ci-dessous

 UIBezierPath *maskPath; maskPath = [UIBezierPath bezierPathWithRoundedRect:view1.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(5.0, 5.0)]; CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.view.bounds; maskLayer.path = maskPath.CGPath; view1.layer.mask = maskLayer; 

La sortie est:

entrez la description de l'image ici

Trouvé ce morceau de code. Je n'ai pas essayé, mais il semble que c'est ce dont vous avez besoin.

 - (void)drawDashedBorderAroundView:(UIView *)v { //border definitions CGFloat cornerRadius = 10; CGFloat borderWidth = 2; NSInteger dashPattern1 = 8; NSInteger dashPattern2 = 8; UIColor *lineColor = [UIColor orangeColor]; //drawing CGRect frame = v.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 [v.layer addSublayer:_shapeLayer]; v.layer.cornerRadius = cornerRadius; } 

Ce morceau de code ajoute une ligne en pointillé, mais vous pouvez le modifier avec _shapeLayer.lineDashPattern .

À less qu'il y ait une exigence spécifique dont nous ne sums pas conscients, le path du Bézier et le masque sont inutiles si tout ce que vous essayez de faire est de contourner les coins et d'avoir une bordure. Je ferais normalement juste ceci: myView.layer.borderWidth = 2; myView.layer.cornerRadius = 5;

Est-ce que vous ne voulez que les coins supérieurs arrondis que vous ne devez pas utiliser l'arrondi de la couche? Si oui, pourquoi ne pas l'utiliser, puis recouvrir une vue fine pour couvrir le bit de fond? Un peu compliqué, mais je trouve que plus vous pouvez countr sur les controls standard pour dessiner eux-mêmes plutôt que d'avoir à entrer dans les charts de base, mieux c'est.

Edit : ok, étant donné qu'il doit avoir les coins inférieurs non arrondis, que diriez-vous si vous aviez une catégorie sur UIView avec 2 sous-vues: 1 avec 4 coins arrondis et un autre sur le dessus (self bringSubviewToFront) qui couvre simplement la vue arrondie "pied de page" avec une bande non arrondie, c'est-à-dire une vue avec une largeur égale et une hauteur minuscule qui est égale au rayon du coin arrondi. Si vous disposez d'un arrière-plan de couleur uni, faites en sorte que les deux sous-vues soient identiques. Si vous avez un fond de texture ou d'image, rendez-les transparents et placez la texture / l'image dans la super vue (la vue parente qui utilise la méthode de layout spécifique de votre catégorie). Ensuite, mettez la bordure sur cette même vue. Devrait travailler, laissez-moi savoir ce que vous pensez.