Reputation: 51
I have added an image to my viewcontroller in code. Without the use of an imageview. This is how i did it:
UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,100,500)];
backgroundImage.image=[UIImage imageNamed:@"backgroundImage"];
[self.view addSubview:backgroundImage];
There are 2 things i need a litte help with. How can i get the image to the back of the view, behind the labels, buttons etc. And how do i get the image to fit all iphone screen sizes? I hope you can help me. Much thanks
Upvotes: 1
Views: 571
Reputation: 189
Simply get screen size of device by following method to make it compatible to all device.
CGRect rect = [UIScreen mainScreen].bounds;
create UIImageView object of that frame size and insert at index 0 of UIView.
UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:rect]; // This will set image fit to all iphone...
backgroundImage.image=[UIImage imageNamed:@"your_image.png"];
[self.view insertSubview:backgroundImage atIndex:0];
Upvotes: 0
Reputation: 676
Just replace your code with this :
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
backgroundImage.image = [UIImage imageNamed:@"backgroundImage"];
[self.view insertView:backgroundImage atIndex:0];
Hope this helps. Let me know it.. :)
Upvotes: 0
Reputation: 5967
As you asked two questions-
Question 1 : How to get the labels/ buttons other subviews visible over the image?
Answer -
Just add image before any other subview.
And there are two ways to set a background image to a UIView
–
You can set your view
background color to color created with UIColor’s
colorWithPaternImage
.
You can add a UIImageView
subview
to your view
.
colorWithPaternImage
was created to use small images to create a pattern image that will be repeated. Using it with large images wont be a wise decision. So if you want to have a patterned image background with uses small images which will be repeated to fill the background, then you should use colorWithPaternImage
, as it will do it quickly and wont consume much memory.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
If you have a full size background image, then you should definitely use the UIImageView
. Using UIImageView
will save a lot of memory in this case.
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
[self.view addSubview:backgroundView];
Question 2 : How to show the image as per the device/ screen resolution?
Answer -
CGRect screenBounds = [[UIScreen mainScreen] bounds];
Use the above screenBounds
to set the frame of ImageView
.
Upvotes: 2
Reputation: 996
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroundImage"]];
Upvotes: 0
Reputation: 1214
UIImageView *backgroundImage =[[UIImageView alloc] initWithFrame:self.view.bounds]; // This will set image fit to all iphone...
backgroundImage.image=[UIImage imageNamed:@"your_image.png"];
[self.view addSubview:backgroundImage];
// This line will send image behind all the other controls
[backgroundImage sendSubviewToBack:self.view];
Upvotes: -1