colorWithPatternImage 和 setCornerRadius 问题
您好,我想同时实现圆角和由平铺一点 png 组成的背景(OPERATOR_VIEW_BACKGROUND_IMAGE
)。我的主要目标是允许设计人员通过在项目资源中插入正确的图像来填充视图的背景。
[triggerView setFrame:CGRectMake(0, 0, ICONS_WIDTH, iconFrameHeight)];
[triggerView.layer setCornerRadius:borderRadius];
[triggerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]]];;
我不知道为什么,但是当我添加最后一行时,triggerView
松开了 CornerRadius
设置。 triggerView
是一个使用界面构建器构建的 UIView
,并通过上面的代码以编程方式在其 superView viewDidLoad
中进行修改。
我哪里错了?
编辑:我没有提到如果我使用简单的 UIColor
例如: [UIColor OrangeColor]
它效果很好。所以它与 patternImage
相关。
编辑:我也尝试过这段代码,在我的视图的图层背景上工作:
[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];
现在我得到一个透明的背景,但角是圆角的;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,感谢 Ben 的评论和这个 博客文章 我找到了这个解决方案:
似乎
triggerView.layer.masksToBounds = YES;
是缺失的部分,但我仍然不明白为什么要触发View.layer。 cornerRadius = radius;
单独是不够的。OK, thanks to Ben's comment and this Blog entry I've found this solution:
Seems that
triggerView.layer.masksToBounds = YES;
was the missing piece, but I still don't understandwhy triggerView.layer.cornerRadius = radius;
alone didn't suffice.尝试使用图像的 CGImageRef 设置图层的内容属性:
您可能必须分配或保留 UIImage 以防止它自动释放...
Try setting the content property of the layer with the CGImageRef of the image:
You may have to alloc or retain the UIImage to prevent it being autoreleased...
在 iOS SDK 4.0 和 4.1 中,
colorWithPatternImage
方法存在一个错误,该错误会导致图像显示效果很差...我使用了这种方法,但遇到了这个错误,但我可以使用另一种方法解决...
尝试使用
UIColor
类的initWithPatternImage
方法:这对我来说非常有用......
不幸的是我从未使用过
cornerRadius
方法,而且我也没有使用知道另一个问题的可能解决方案。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 ofUIColor
class:This worked greatly for me...
Unfortunately I never used
cornerRadius
method and I don't know a possibly solution for this other problem.