how to place a UIImageView in a particular area of a subView - iphone

I am woundering if there is a way of placing a UIImageView into a specific area of another view using addSubview
this is how my code currently looks, I am woundering how I would add the image to a particular spot of the view its being added to, say (0,0) top left.
this is what I am doing atm.
//...
jumpBar = [[JumpBarViewController alloc] initWithNibName:#"JumpBarViewController" bundle:[NSBundle mainBundle]];
jumpBarContainer = [[UIView alloc] initWithFrame:CGRectMake(0.0, 480.0, (jumpBarHeight + 49.0), 320.0)];
[jumpBarContainer addSubview:jumpBar.view];
[self.view insertSubview:jumpBarContainer belowSubview:actionTabBar];
// Add jumpBar shade
switch (jumpBarHeight) {
case 103:
{
NSLog(#"one row");
UIImageView *smlShader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1-row-top-bg.png"]];
[jumpBarContainer addSubview:smlShader]; // how do i add this to the top left of jumBarContainer?
}
//...
any help would be greatly appreciated :)

Set the UIImageView frame:
UIImageView *smlShader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1-row-top-bg.png"]];
smlShader.frame = CGRectMake(x,y,w,h); //Change the value of the frame
[jumpBarContainer addSubview:smlShader];

Related

How to set UIImageView frame in ViewController for Universal App?

For a Universal App, how do I set the UIImageView frame in the ViewController?
UIView *baseView = [[UIView alloc] init];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
[baseView addSubview:myImage];
When I run app it shows black screen. So i guess I have to set a frame for it.
You kinda answered your own question - yes you have to set a frame for it.
You may want to try:
UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
myImage.frame = baseView.bounds;
[baseView addSubview:myImage];
You could also set the autoresizingmask for baseView and myImage so that as self.view's frame changes, so do these two views.
Also, just in case you aren't using ARC - don't forget to autorelease these views before leaving this method.

Make a rectangle equal to image view

I have an imageView. I need to restrict the size of imageView to the white background imageOnly.The image is tightly bounded between right,left and top margin. I am confused on how to do at bottom, such that the image must fit in the white background only. How do I do that ?
http://i.stack.imgur.com/ztvQe.png
The code I am using
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 240,175,205)];
Frame width and height is 205*205
You can add your imageView as a subview of the view with the white background.
UIView *whiteBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(25,240,205,205)];
CGSize whiteSize = whiteBackgroundView.frame.size;
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,size.width,size.height)];
[whiteBackgroundView addSubview:_imageView];
In the example your imageView will completely fit the size of the white background view.
Try this
UIView *productView = [[UIView alloc]initWithFrame:CGRectMake(25, 240,225,225)];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,205,205)];
_imageView.clipsToBounds =YES;
[productView addSubview:_imageView];
[self.view addSubview:ImageBackview];
just create one view and add this image in it, for example use bellow code..
UIView *ImageBackview = [[UIView alloc]initWithFrame:CGRectMake(25, 240,225,225)];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10,205,205)];
[ImageBackview addSubview:_imageView];
[self.view addSubview:ImageBackview];

How to add two uiscrollviews as subview

After lot of research and working for days, I really need some guidance here. I am trying to add uiscrollview on top of each other. I have two UIScrollViews: ImageScrollView and MarkScrollView. I want to add MarkScrollView as subview to ImageScrollView. ImageScrollView is added to UIViewController MapImageViewController.
In ImageScrollView it consists of images which are in tiledLayer. In MarkScrollView, it consists of imageview which has a pointer image. But for some reason it cannot be added as subview to other view. I have added my code. If anybody can help me to figure out the error it will be really appreciated.
/** ImageScrollView.m It adds uiview on top of ImageScrollView***/
UIView *imageView = [[TileView alloc] initWithImageName:imageName size:imageSize];
[(TileView *)imageView setAnnotates:NO]; // ** remove this line to remove the white tile grid **
[self addSubview:imageView];
/** MarkScrollView.m It adds uiview with uiimages on top of MarkScrollView**/
UIImage *markerimage = [UIImage imageNamed:#"map.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: markerimage];
imageView.image = markerimage;
[imageView setFrame:CGRectMake(100,100,50,50)];
imageview = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
[imageview setBackgroundColor:[UIColor blackColor]];
[imageview addSubview: imageView];
[self addSubview:imageview];
/** MapImageViewController.m It adds ImageScrollView on top of another UIScrollView pagingScrollView **/
UIScrollView *pagingScrollView;
ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
[pagingScrollView addSubview:page];
MarkScrollView *markerScroll = [[[MarkScrollView alloc] init] autorelease];
[page addSubview:markerScroll]; /**But it never adds the other scrollview here **/
/** MapImageViewController.h **/
#interface MapImageViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate> {
ImageScrollingView *imageScrollView;
MarkerIconsScrollView *markerScrollView;
}
UIScrollView *pagingScrollView;
ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
[pagingScrollView addSubview:page];
MarkScrollView *markerScroll = [[[MarkScrollView alloc] init] autorelease];
[page addSubview:markerScroll]; /**But it never adds the other scrollview here **/
I'm noticing some things on this code snippet:
pagingScrollView is never initialized
the views that you are adding to it (provided that it actually is initialized with a valid frame), are not initialized with a frame. Always use - (id)initWithFrame:(CGRect)aRect to initialize your UIViews
Good luck!

iPhone: Display image at its original size

I am trying to set frame for imageview to original size of image and display it in center of view. I have written following code for this but image still appears in whole screen! Suppose image size is 320x88, it appears resized/stretched to 320x460! I want it to appear in center of screen so I set Y coordinate to (460-88)/2 but it doesn't work. Could anyone please help me to resolve this. Thanks.
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
if(imageView.image.size.width == IPHONE4_RES_WIDTH)
[imageView setFrame: CGRectMake(0, 0, imageView.image.size.width/2, imageView.image.size.height/2)];
UIViewController *viewController = [[[UIViewController alloc] init] autorelease];
viewController.view.backgroundColor = [UIColor blackColor];
NSLog(#"%f : %f",viewController.view.bounds.size.height, imageView.bounds.size.height);
viewController.view = imageView;
viewController.view.contentMode = UIViewContentModeCenter;
[self.navigationController pushViewController:viewController animated:YES];
try this
viewController.view.contentMode=UIViewContentModeCenter;
you can set the uiimageview to not stretch the image.. even if it is full size it will show the image in the center when you set
imageView.contentMode = UIViewContentModeCenter
Ran across this question while trying to achieve the same in a storyboard file. Just as an addition: in storyboard for the same thing you can select Mode = Center of UIImageView
Following works perfectly for my requirements!
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
// Reduce the width and height for iPhone 4 res
if(imageView.image.size.width == IPHONE4_RES_WIDTH)
[imageView setFrame: CGRectMake(0, 0, imageView.image.size.width/2, imageView.image.size.height/2)];
UIViewController *viewController = [[[UIViewController alloc] init] autorelease];
viewController.view.backgroundColor = [UIColor blackColor];
float X = (viewController.view.bounds.size.width - imageView.bounds.size.width)/2;
float Y = (viewController.view.bounds.size.height - imageView.bounds.size.height)/2 - 50; // Size of tabbar and navigation bar deducted
NSLog(#"(%f, %f) : (%f,%f)",X,Y,imageView.bounds.size.width,imageView.bounds.size.height);
[viewController.view addSubview:imageView];
//viewController.view = imageView;
//viewController.view.contentMode = UIViewContentModeCenter;
// Set in the center of the screen
if(imageView.image.size.width == IPHONE4_RES_WIDTH)
[imageView setFrame: CGRectMake(X, Y, imageView.image.size.width/2, imageView.image.size.height/2)];
else
[imageView setFrame: CGRectMake(X, Y, imageView.image.size.width, imageView.image.size.height)];
[self.navigationController pushViewController:viewController animated:YES];

How to add an image subview to a tableview programmatically

I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.