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
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.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundImage"]];
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.. :)
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];
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];
Related
I created an UIView.
I need to set a small image on a particular position.
I can do it setting the image in a small separate view.
But, I plan to do it dynamically on a Large UIView which is full screen.
Thanks.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,200)];
view.backgroundColor=[UIColor clearColor];
[self.view addSubview:view];
// add image in view
UIImageView *imageView =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
imageView.image=[UIImage imageNamed:#"image.png"];
[view addSubview:imageView];
That is what views are for: manipulating UI widgets.
By far, your best approach is to position a UIImageView.
You can also override the view drawing methods:
override func drawRect(rect: CGRect) {
// Curstom
}
Try this code.
UIImage *image = [UIImage imageNamed:#"imagename.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//specify the frame of the imageView in the superview , here it will fill the superview
imageView.frame = catView.bounds;
// set custom frame as per your requirement
//or
// imageView.frame = CGRectMake(10,10,30,30);
//or
//according to superviews frame
//imageView.frame=CGRectMake(10,10,yourUIViewObject.bounds.size.width/2,yourUIViewObject.bounds.size.height/2);
// add the imageview to the superview
[yourUIViewObject addSubview:imageView];
How to do programmatically ImageView with same size of View in a way that there is no difference between devices ?
UIImageView *imgV =[[UIImageView alloc] initWithFrame:self.view.frame];
imgV.image=[UIImage imageNamed:#"draw.png"];
[self.view addSubview:imgV];
suppose draw.png is xx size then draw#2x = 2(xx), draw#x = 3(x*x)
please supply image draw.png, draw#2x.png , draw#3x.png in resource
hope it help
With code you can create a UIImageView with giving the frame of superview.
UIImageView* myImageView = [[UIImageView alloc]initWithFrame:self.view.frame];
[myImageView setImage:[UIImage imageNamed:#"yourImage.jpg"]];
[self.view addSubview:myImageView];
By Autolayout using storyboards we can do like this.
Drag an UIImageView to the view and provide these constraints and add.
I'm developing a iOs app for iPad and I'm implementing a UIImageView inside a UIView. The problem is that if the position of the image view has a position out of the UIView that it is inside, you can see it. The code:
UIView *vistafunda = [[UIView alloc] initWithFrame:CGRectMake(512/2, 48, 525, 651)];
[self.view addSubview:vistafunda];
vistafunda.backgroundColor = [UIColor darkGrayColor];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hola.png"]];
img.center = CGPointMake(12, 200);
[vistafunda addSubview:img];
I would like that the image view was always under the view, so if the image is outside the view you could not see it.
Thanks!
Just make the containing view clip it's children
vistafunda.clipsToBounds = YES;
I have an iPad/iPhone app and the iPhone background looks fine, but when I use the same image for the iPad version, it looks like it is getting cut off in the middle of the page.
Here is a screen shot:
The image is called buildings.png and I have 2 versions of it, the buildings.png and the buildings#2x for the iPad.
The way I get the image to render in the background is from the code like this:
- (void)viewDidAppear:(BOOL)animated
{
UIImage *image = [UIImage imageNamed:#"building"];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
What would be the best way for me to fix this issue of the image being cut off like that on the iPad view?
Thanks!
I think the colorWithPatternImage is the reason your image is getting cut like that. Why not create a UIImageView to contain your image, add it on your view and call [self.view sendSubviewToBack:yourImageView] ?
EDIT
Here is your code snippet
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"building"]];
imgView.frame = self.view.bounds; // to set the frame to your view's size
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
This is what worked for me -
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"]];
imgView.frame = [[UIScreen mainScreen] bounds]; // to set the frame to your view's size
[self.view addSubview:imgView];
[self.view sendSubviewToBack:imgView];
I have the following code in ViewDidLoad of my UiTableViewController:
UIImage *noImage = [UIImage imageWithContentsOfFile:pathOfNoImageFile];
UIImageView *imageview = [[UIImageView alloc] initWithImage:noImage];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
imageview.frame = workingFrame;
[self.scrollview addSubview:imageview];
workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;
[self.scrollview setPagingEnabled:YES];
[self.scrollview setContentSize:
CGSizeMake(workingFrame.origin.x,workingFrame.size.height)];
I therefore am only able to see the image partially and have attached a screen shot.
What can i do to fix it? I have Googled and looked at several Stack Overflow questions but have not been able to have it work. Please suggest. I have a scrollview in the uitableviewcell. I need to center my image vertically .
Your scrollview/cell size will need to be big enough to hold the imageview that you want to display. Looks like one or both is too small so I would adjust your sizes accordingly.