Directing Xcode Image into image view - xcode

Hi Quick question really currently I'm trying to connect my image to my image view within Xcode and currently when I run the program y image appears but it does so in the top left hand corner, I just wanted to know what line of code I was missing in order to navigate my image directly into one of my images views.
Thanks
UIImageView *myImage = [[UIImageView alloc]
//Allocating space for the image
initWithImage:[UIImage imageNamed:#"TESTPIC.jpg"]];
//Selecting the image
[self.view addSubview:myImage];
//Displaying the Image

You can use the frame property of UIImageView. Please find the below code for more information.
UIImageView *myImage = [[UIImageView alloc]
//Allocating space for the image
initWithImage:[UIImage imageNamed:#"test.png"]];
myImage.frame = CGRectMake(your x position, your y position, myImage.frame.size.width, myImage.frame.size.height);
//Selecting the image
[self.view addSubview:myImage];
Hope it works.

Related

Send UIImageView (label background) to back to display UILabel

I am trying to add an image as a background to a UILabel, but my UILabel's title cannot be seen, even though I tried to send the background image to the back. My code is below and any advice on how to help with this would be great, thanks!
UIImageView *labelBackground = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
[myLabel addSubview:labelBackground];
[myLabel sendSubviewToBack:labelBackground];
myLabel.backgroundColor = [UIColor clearColor];
[myLabel setText:title];
When you add a view (your image view) as a subview to another view (your label), the subview will always be in front of its superview. They would either need to be siblings:
[myContainer addSubview:labelBackground];
[myContainer addSubview:myLabel];
or better yet, the label should be a subview of the image view:
[labelBackground addSubview:myLabel];
[myContainer addSubView:labelBackground];
Another solution might be to use your image as a background color for your label:
[myLabel setBackgroundColor:[UIColor colorWithPatternImage:myUIImage]];
But note that the image will be repeated instead of centered or stretched.
Try adding the UILabel as a subview of the UIImageView, rather than the other way around.
This will result in the UIImageView being the "container" of the label, and thus the label being on top of the UIImageView.

How do i make a simple scrollview height = 80, width = 280, with images inside thats scrolls horizontally?

I need to make a simple scroll view in xcode with width of 280 and height of 80 and with images inside thats scrolls horizontally. i want to make this programmatically.
I assume you mean the UIScrollview, which has a guide written by apple found here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIScrollView_Class/Reference/UIScrollView.html
A guide that I personally used was this one:
http://idevzilla.com/2010/09/16/uiscrollview-a-really-simple-tutorial/
I'll take you through the quick basics of adding the scrollview to your view and adding images to it.
I'm guessing you're new to Objective C, so I'll give you a quick guide. Firstly, you'll want to make a UIScrollView object. This is done by declaring the following:
UIScrollView *aScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake (0,0,320,250)];
You'll notice I set the frame. The first two numbers of CGRectMake give you the x and y origin of the point while the last two numbers are for how wide and tall you want your object to be.
Afterwards, you'll want to add images to it. You'll need a UIImageview.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 250)];
Note that I positioned the image at 0,0, giving it a height of a 250 and a width of 320. This ensures that it fills entire scrollview's initial view.
imageView.image = [UIImage imageNamed:#"foo.png"];
You'll attach an image to the imageView. But wait, there's more. So far you've created these objects but have not yet associated them with the view. So if we are in a ViewController class (you'll have to look up what that is), the ViewController contains a view. We can attach our objects to the view.
[aScrollView addSubview:imageView]; // Adds the image to the scrollview
[self.view addSubview:aScrollView]; // Adds the scrollview to the view.
If you want to add more images, you have to add them at different x origins. So our first added image was at 0,0. Our next added image should be at 320,0 (because the first image took up 320 pixels width).
UIImageView *secondImageView = [[UIImageView alloc] initWithFrame:CGRectMake(320, 0, 320, 250)];
secondImageView.image = [UIImage imageNamed:#"bar.png"];
[aScrollView addSubview:secondImageView];
There are a number of options for scrollview that you will want to explore. The ones I found useful were:
aScrollView.delegate = self; // For gesture callbacks
self.pagingEnabled = TRUE; // For one-at-a-time flick scrolling
self.showsHorizontalScrollIndicator = NO; // Cleaner look for some apps.
self.alwaysBounceHorizontal = TRUE; // Look it up.

Place a background Image behind a grouped UITableView

I've a grouped UITableView which has been resized and rounded. I would like to place a view BEHIND this table.I've tried:
[self.tableView addSubview:backgroundView];
[self.tableView sendSubviewToBack:backgroundView];
but it didn't work. This is what I obtain:
I would like to have the background in place of the black space.
Any idea?
How about [self.tableView setBackgroundView:backgroundView];?
Bear with me I'm a beginner. I just ran into this problem and I found two solutions. In my case, I was using a UITableView object.
To begin, add the background image into your app's directory. I put mine in "Supporting Files"
You can add the image as a UIColor object:
...
[resultsTable setBackGroundColor:setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.jpg"]]];
...
The problem with this method is that it will repeat your background image because it's treating it as a pattern.
The better way:
// Create a string to store the path location of your image, otherwise you would have to provide an exact path name
NSString *backPath = [[NSBundle mainBundle] pathForResource:#"background"
ofType:#"jpg"];
// Create a UIImage Object to store the image.
UIImage *bgImage = [[UIImage alloc ] initWithContentsOfFile:backPath];
// Create a UIImageView which the TableView
UIImageView *bgView = [[UIImageView alloc]initWithImage:bgImage];
// Set your TableView's background property, it takes a UIView Obj.
[resultsTable setBackgroundView: bgView];
.....

UINavigationBar with image and default gradient background with iOS5

I'm attempting to use the new [UINavigationBar appearance] functionality in iOS5 to add a logo image to the UINavigationBars in my application. Primarily, I'd like to keep the default gradient, but center a transparent png in the NavBar. The logo image is roughly 120 pixels wide (240 pixels#2x).
I have first attempted this by setting the background image. The default behavior for setBackgroundImage:forBarMetrics: appears to be to tile the image, and all transparent parts show the default navbar background color, black. I can also set the background color via the appearance modifier, and get a flat color background, but I'd really like to get the original gradient behavior without maintaining a separate image resource for it. It also makes it easier to adjust in code, since I can adjust the tint there, rather than re-generating a new image if I decide to change it.
What I'm trying to use:
UIImage *logoImage = [UIImage imageNamed:#"logoImage"];
[[UINavigationBar appearance] setBackgroundImage:logoImage forBarMetrics:UIBarMetricsDefault];
You can do this two ways. If you want to always have the image in the navigation bar, then create an image view and set it as a subview of the navigation bar:
[self setLogoImageView:[[UIImageView alloc] init]];
[logoImageView setImage:[UIImage imageNamed:#"logo.png"]];
[logoImageView setContentMode:UIViewContentModeScaleAspectFit];
CGRect navFrame = [[navController navigationBar] frame];
float imageViewHeight = navFrame.size.height - 9;
float x_pos = navFrame.origin.x + navFrame.size.width/2 - 111/2;
float y_pos = navFrame.size.height/2 - imageViewHeight/2.0;
CGRect logoFrame = CGRectMake(x_pos, y_pos, 111, imageViewHeight);
[logoImageView setFrame:logoFrame];
[[[self navigationController] navigationBar] addSubview:logoImageView];
If you only want to display the logo in a certain view, then set the view's navigation item:
[logoImageView setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
[[self navigationItem] setTitleView:logoImageView];

Randomize UIImageViews through code

I have 8 images on my View. 4 on left n 4 on right side on the view. i need to match images every time from ones on left to ones on right. I have done this statically by creating 8 image views in the interface builder and connecting it to the outlets created. Now i have to dynamically randomize the image views so tat the match doesn't become obvious. like for the 1st left topmost imageview the match will always be at the bottom most in the right side image view. i tried creating image views in 2 formats
UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRect(111, 135,140,130)];
image1.image = [UIImage imageNamed:#"iMac.png"
also
UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"iMac.png"]];
image1.frame = CGRect(111,135,140,130);
images are not displayed on the screen at all. Please correct me if i have gone wrong in the code
Just Replace
image1.frame = CGRect(111,135,140,130);
with
image1.frame = CGRectMake(111,135,140,130);

Resources