colorWithPatternImage和setCornerRadius问题

时间:2010-10-08 16:29:27

标签: iphone objective-c rounded-corners

您好我想同时实现圆角和通过平铺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];

现在我得到一个透明的背景,但角落是圆的;

3 个答案:

答案 0 :(得分:4)

好的,感谢Ben的评论和Blog entry我找到了这个解决方案:

 [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;是缺失的部分,但我仍然不明白仅why triggerView.layer.cornerRadius = radius;是不够的。

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

使用iOS SDK 4.0和4.1时,colorWithPatternImage方法有一个错误显示图像的错误...
我使用这种方法,我有这个错误,但我可以解决使用另一种方法...
尝试使用initWithPatternImage类的UIColor方法:

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

这对我很有帮助......

不幸的是我从未使用过cornerRadius方法,而且我不知道这个问题的解决方案。

相关问题