In iPhone SDK, there is no setBackgroundImage function for setting an background image of an UIView. But actually, this can be done by the setBackgroundColor function. The following code will set the background view to Red.
self.view.backgroundColor = [UIColor redColor];
So if you want to set a picture as an background image of a view, just initialize an UIColor instance with an image and set it to the view.
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"anImage.png"]]; self.view.backgroundColor = background; [background release];
Please note that you have to set all the items which are in front of the background view to clearColor such that they will not cover the background image.
Done =)
Reference: Use image for background for UIView
Thx a lot.
you are welcome =)
Thanks
=)
hey ykyuen thanx for the post.
I problem I am getting is images are not taking transparency in uiview.
did you set the uiview.opague to NO and change the uiview.alpha value?
UIView Class Reference
Holy cow. That’s awesome. Thank you.
haha~~ you are welcome =D
hey buddy, thank you so much!
you are welcome. =)
This did not work for my iPad view, what could be the problem ? I am doing this in viewDidLoad() …
Is the following work?
If yes, then i think there should be some problem when initializing a UIColor instance with image. Check if the image is loaded without problem.
This worked great for me, just one issue…the image being displayed on my iPhone’s screen is coming out lighter/duller looking than the original png. Any thoughts on what the cause of this might be?
i didn’t look into this issue before. do u have anything on top of the background? if you are working with table view, the following tutorial may help but i am not sure if that is ur problem.
How To Add A Nice Background Image To Your Grouped Table View
Yes. You are right to set background image. But i have some doubt in it. When we apply pinch gestureRecognizer on view , the background image is also increasing with view but here only increase the view and not on background image. Is it possible ? if yes then please replay to me how to solve it.
so you only want to increase the view size but not the background image?
do you think using a UIImageView instead of setting the background image of a UIView could solve your problem?
StackOverflow – GestureRecognizer on UIImageView
Hello!
Thanks.
I`d like to ask, is there any method to scale background image to UIView size. I`ve done like here, but my image is too big for view. Do U know such ?
Maybe you could try this
Reference:
Just wanted to say how great this little tip is! Thanks for sharing!
Thanks for your comment and good to know that i could help.
Thanks for the article. I just want to ask that how to set background image to table view??
Try this
Reference: StackOverflow – iPhone – Setting background on UITableViewController
This works for me…
Thanks
You are welcome =)
Hello, your method is very good. Thanks a lot.
But I have a problem now.
My image is large than my iphone simulator screen,I can’t see the whole image.
How can I do?
Does it help?
Reference: StackOverflow – How to fill background image of an UIView
Yes, it works. Thank you.
But I find another method.
How do you think?
UIImage *bg = [UIImage imageNamed:@"image.png"]; UIImageView *menu_bg = [[UIImageView alloc] initWithImage:bg]; CGSize screenSize = [UIScreen mainScreen].bounds.size; menu_bg.frame = CGRectMake(0, 0,screenSize.width ,screenSize.height ); [self.view addSubview:menu_bg];I am not very familiar with iOS development so i couldn’t comment on which one is better. Your approach should have no problem i think,
Anyway, thanks for providing your solution here. =)
can that image be stretchable image ? i used this property to stretch image “stretchableImageWithLeftCapWidth” but its not working .. how do set a stretchable image as background ?
Try this
Reference: StackOverflow – How to fill background image of an UIView
i want to place an background image of my view, which is already a designed view…
for example i develop my login screen with fields email,pwd,confirm pwd,submit button..
how can i add an image as my view’s background…????
pls answer me
Have you tried to set the background color of the designed view as suggested in this post?
what problem do u have?
Hello, all. I have some strange issue:
The following post should help you to deal with the retina devices.
StackOverflow – iOS Retina display: images double size