views:

46

answers:

3

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 by 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;

+1  A: 

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...

Ben
No it doesn't work. BTW you need to delete the square bracket at the beginning.
microspino
Oops thanks for the fix! This does work for me when I create a layer, set its corner radius then set its background color to clear and its masksToBounds property to yes. I then create ANOTHER layer with the image as its contents and add this as a sublayer to achieve the effect.
Ben
A: 

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... Sorry!

However I hope this answers can help you ;)

Matthew
I've used initWithPatternImage before colorWithPatternImage, It doesn't work and I prefer the latter to avoid adding another object to release with alloc. Anyway THX.
microspino
A: 

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];

triggerView.layer.masksToBounds = YES; was the missing piece but I still don't understand why triggerView.layer.cornerRadius = radius; alone doesn't suffice.

microspino