Hello I'd like to achieve at the same time rounded corners and a background composed by tiling a little png (OPERATOR_VIEW_BACKGROUND_IMAGE
). My main goal is to allow a designer to fill the background of a View 开发者_StackOverflowby inserting the right image in the project resources.
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
[triggerView.layer setCornerRadius:borderRadius];
[triggerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]]];;
I don't know why but triggerView
loose the CornerRadius
setting when I add the last line.
triggerView
is a UIView
built with interface builder and modified in its superView viewDidLoad
programmatically, with the code above.
Where I'm wrong?
EDIT: I haven't mentioned that If I use a simple UIColor
like: [UIColor orangeColor]
It works well. So It's something related to the patternImage
thing.
EDIT: I've tried also this code, working on the layer background of my view:
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
triggerView.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE];
triggerView.layer.backgroundColor = [UIColor colorWithPatternImage:img].CGColor;
triggerView.layer.cornerRadius = radius;
[img release];
[self.view addSubview:triggerView];
Now I get a transparent background but the corners are rounded;
OK, thanks to Ben's comment and this Blog entry I've found this solution:
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
triggerView.layer.masksToBounds = YES;
triggerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]];
triggerView.layer.cornerRadius = radius;
[self.view addSubview:triggerView];
Seems that triggerView.layer.masksToBounds = YES;
was the missing piece, but I still don't understand why triggerView.layer.cornerRadius = radius;
alone didn't suffice.
Try setting the content property of the layer with the CGImageRef of the image:
triggerView.layer.contents = (id) [[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE] CGImage];
You may have to alloc or retain the UIImage to prevent it being autoreleased...
With iOS SDK 4.0 and 4.1, colorWithPatternImage
method has got a bug that show badly an image...
I used this method and I had this bug but I could resolve using another method...
Try to use the initWithPatternImage
method of UIColor
class:
UIColor *imageBg = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]];
This worked greatly for me...
Unfortunately I never used cornerRadius
method and I don't know a possibly solution for this other problem.
精彩评论