中心[UIColor colorWithPatternImage]?

时间:2011-09-30 07:15:05

标签: uiview uiimage calayer uicolor

我找不到任何关于你在使用时如何使'图像'居中的事情(也许你不能):

[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]]];

设置我的[self setContentMode:UIContentModeCenter];没有帮助。

我是否必须手动绘制图像(在drawRect中设置或设置CALayer的内容?哪一个更合适?

1 个答案:

答案 0 :(得分:2)

我认为你走错了路:你从一个模式创建一个UIColor(模式已经暗示这是一个重复的图像)。总而言之 - 你不能让你的模式不重复和居中。

如果您只想将简单图片作为UIView的背景,只需将其添加为子视图并将其居中。

UIImage* img = [UIImage imageNamed:@"yourfile.png"];
UIImageView* imgView = [[UIImageView alloc]initWithImage: img];

[yourUIView addSubview: imgView];
imgView.center = CGPointMake(yourUIView.frame.size.width/2, yourUIView.frame.size.height/2);

现在 - 在“yourUIView”视图中添加更多子视图,它们会显示在图片的顶部 - 因此图像会成为背景。

所以......不需要自己画任何东西。

相关问题