Setting a jpg as the background for a UIView?
I know this is a very beginner question, but I'm obviously a beginner. I have already made a my TabBar but I want to set the background of one of the views as a (jpg) I created. I need to add the background in code (not IB) so that I can allow rotation and resizing when th开发者_运维百科e iphone is rotated.
-thanks
You need to use a UIImageView
, which is a subclass of UIView
. You can create it as follows:
UIImageView *myImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage.jpg"]];
[self.view addSubview:myImage];
[myImage release];
...so here I've created a UIImageView that uses a JPG called 'myImage' (it will automatically resize the view to fit the image), added it to my view controller, and then cleaned up my memory.
You can try this one to set an image as a background for a view programmatically:
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"imageName.png"]]
Add an instance of UIImageView to your view, and set it's image to an instance of UIImage created using your file.
You can add the image view in IB or in code -- there's nothing about doing it in IB that would prevent you from resizing, rotating, etc.
精彩评论