文
UIColor colorWithPatternImage代码片段(背景平铺)
章来源:http://blog.csdn.net/wave_1102/article/details/5180958
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Checkers.png"]];
Center [UIColor colorWithPatternImage]?
I couldn't find anything on how you can center the 'image' when you use (maybe you can't):
[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]]];
Setting my [self setContentMode:UIContentModeCenter];
doesnt help.
Do I have to manually draw the image (either in drawRect
or set the content of the CALayer
? Which one if preferable?
1 Answer
I think you're on the wrong path here: you create a UIColor
from a pattern (pattern already implies this is a repeating image). All in all- you can't have your pattern not repeat and centered.
If you just want simple image as background of your UIView
, just add it as a subview and center it.
UIImage* img = [UIImage imageNamed:@"yourfile.png"];
UIIImageView* imgView = [[UIImageView alloc]initWithImage: img];
[yourUIView addSubview: imgView];
imgView.center = CGPointMake(yourUIView.frame.size.width/2, yourUIView.frame.size.height/2);
Now - add more subviews to your "yourUIView
" view and they'll show on top of the image - thus the image becoming the background.
So... no need to draw anything yourself.
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Checkers.png"]];
Center [UIColor colorWithPatternImage]?
I couldn't find anything on how you can center the 'image' when you use (maybe you can't):
[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]]];
Setting my [self setContentMode:UIContentModeCenter];
doesnt help.
Do I have to manually draw the image (either in drawRect
or set the content of the CALayer
? Which one if preferable?
1 Answer
I think you're on the wrong path here: you create a UIColor
from a pattern (pattern already implies this is a repeating image). All in all- you can't have your pattern not repeat and centered.
If you just want simple image as background of your UIView
, just add it as a subview and center it.
UIImage* img = [UIImage imageNamed:@"yourfile.png"];
UIIImageView* imgView = [[UIImageView alloc]initWithImage: img];
[yourUIView addSubview: imgView];
imgView.center = CGPointMake(yourUIView.frame.size.width/2, yourUIView.frame.size.height/2);
Now - add more subviews to your "yourUIView
" view and they'll show on top of the image - thus the image becoming the background.
So... no need to draw anything yourself.