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.
LikeLike
you are welcome =)
LikeLike
Thanks
LikeLike
=)
LikeLike
hey ykyuen thanx for the post.
I problem I am getting is images are not taking transparency in uiview.
LikeLike
did you set the uiview.opague to NO and change the uiview.alpha value?
UIView Class Reference
LikeLike
Holy cow. That’s awesome. Thank you.
LikeLike
haha~~ you are welcome =D
LikeLike
hey buddy, thank you so much!
LikeLike
you are welcome. =)
LikeLike
This did not work for my iPad view, what could be the problem ? I am doing this in viewDidLoad() …
LikeLike
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.
LikeLike
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?
LikeLike
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
LikeLike
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.
LikeLike
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
LikeLike
Hello!
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 ?
🙂 Thanks.
LikeLike
Maybe you could try this
Reference:
LikeLike
Just wanted to say how great this little tip is! Thanks for sharing!
LikeLike
Thanks for your comment and good to know that i could help. 🙂
LikeLike
Thanks for the article. I just want to ask that how to set background image to table view??
LikeLike
Try this
Reference: StackOverflow – iPhone – Setting background on UITableViewController
LikeLike
This works for me…
Thanks
LikeLike
You are welcome =)
LikeLike
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?
LikeLike
Does it help?
Reference: StackOverflow – How to fill background image of an UIView
LikeLike
Yes, it works. Thank you.
But I find another method.
How do you think?
LikeLike
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. =)
LikeLike
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 ?
LikeLike
Try this
Reference: StackOverflow – How to fill background image of an UIView
LikeLike
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
LikeLike
Have you tried to set the background color of the designed view as suggested in this post?
what problem do u have?
LikeLike
Hello, all. I have some strange issue:
LikeLike
The following post should help you to deal with the retina devices.
StackOverflow – iOS Retina display: images double size
LikeLike