How to set UIImageView frame in ViewController for Universal App? - iphone

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.

Related

How to reset the background of a UIView

After adding a background (image) to my UIView (View_0) I wish to implement a button to remove the same background.
How can I do it? I have tried to set a different image as the background of View_0 but this does not work (I suspect this background is set behind "image").
I don't want to use
[View_0 removeFromSuperview];
as I need View_0 to still be there for other purposes....
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
//Failed attempt to remove the background....
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
You have added a UIImageView to the View and Not the background of the view. So what is happening is that when u implement
View_0.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"defaultImage.PNG"]];
The background of the View is being set but the UIImageView is now on top of the View so that is why the background of the View is not being shown.
Instead if you simply want to add the background you can change it to the Action of the UIButton.
Also if you use [View_0 removeFromSuperview]; you are trying to remove the view which is not right.
//Create an ImageView
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 940, 422)];;
image.tag = 1234;
//Add the ImageView as the background of the View_0
[View_0 addSubview:image];
//Move custom image behind View
[View_0 sendSubviewToBack: image];
UIImageView *imgView = [self.view viewWithTag:1234];
[img setImage:nil];
This should work i think

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

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];

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!

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.

Create a UIImage with UIGestureRecognizer at runtime

I would like to be able to add a new UIImageView of a certain size which should have a few UIGestureRecognizers detecting touch on the UIImageView.
How does one subclass UIImageView or how would this be best achieved?
I want the user to be able to add UIImageViews as they want at runtime and then the gestureRecognizer will allow them to manipulate the views.
Thanks
I think you just forgot to enable userInteractions.
UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"foo.png"]] autorelease];
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction:)] autorelease];
[imageView addGestureRecognizer:tapGesture];
[self.view addSubview:imageView];