colorWithPatternImage 和 setCornerRadius 问题

发布于 2024-09-27 04:13:32 字数 1183 浏览 2 评论 0原文

您好,我想同时实现圆角和由平铺一点 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜血缘 2024-10-04 04:13:33

好的,感谢 Ben 的评论和这个 博客文章 我找到了这个解决方案:

 [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; 是缺失的部分,但我仍然不明白为什么要触发View.layer。 cornerRadius = radius; 单独是不够的。

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.

月棠 2024-10-04 04:13:33

尝试使用图像的 CGImageRef 设置图层的内容属性:

triggerView.layer.contents = (id) [[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE] CGImage];

您可能必须分配或保留 UIImage 以防止它自动释放...

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

冷血 2024-10-04 04:13:33

在 iOS SDK 4.0 和 4.1 中,colorWithPatternImage 方法存在一个错误,该错误会导致图像显示效果很差...
我使用了这种方法,但遇到了这个错误,但我可以使用另一种方法解决...
尝试使用 UIColor 类的 initWithPatternImage 方法:

UIColor *imageBg = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:OPERATOR_VIEW_BACKGROUND_IMAGE]];

这对我来说非常有用......

不幸的是我从未使用过 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文