简体   繁体   中英

Add a CALayer as sublayer centered inside an UIView

I'm developing an iOS application with latest SDK.

I want to add a CALayer inside an UIView and this CALayer will be smaller than the UIView . I want to simulate some kind of margin.

static int const NSLayerX       = 0;
static int const NSLayerY       = 0;
static int const NSLayerWidth   = 60;
static int const NSLayerHeight  = 60;

- (void)setBackgroundLayer:(UIColor*)startColor endColor:(UIColor*)endColor cornerRadius:(int)radius
{
    backgroundGradientLayer  = [CAGradientLayer layer];
    backgroundGradientLayer.frame = CGRectMake(NSLayerX, NSLayerY, NSLayerWidth, NSLayerHeight);
    backgroundGradientLayer.cornerRadius = radius;
    backgroundGradientLayer.colors =
    [NSArray arrayWithObjects:(id)[startColor CGColor], (id)[endColor CGColor], nil];
    [self.layer insertSublayer:backgroundGradientLayer atIndex:0];
}

But I don't know how to set it centered.

Do I have to change values for NSLayerX and NSLayerY?

按如下方式指定图层的框架。

backgroundGradientLayer.frame = CGRectMake(self.frame.size.width/4,  self.frame.size.height/4, self.frame.size.width/2, self.frame.size.height/2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM