sendSubviewToBack hides UIImageView - iphone

I have an UIImageView and a few buttons defined in interface builder. I want to display an image with the buttons on top of it. I add an image to the UIImageView programatically but it covers up the buttons. I tried using sendSubviewToBack and the image disappeared completely. Here's my code
UIImageView *imageView = [[UIImageView alloc] initWithFrame:
CGRectMake(0.0, 0.0, 320.0, 480.0)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"mypic%d.png", index]];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
[imageView release];
Please help.

ok, i'm a newbie and I was dumb. I was defining a UIImageView on interface builder and also in the code thus hiding one behind another. sorry.

You might also choose to simply create the image view in Interface Builder, create an outlet to it, then set the image programmatically. Doesn't look like you need to dynamically mess with the view hierarchy here.

Related

UIStoryboard application and common background image

I have an iOS application which uses an UIStoryboard to control its flow. I would like to have all my views defined in the UIStoryboard to all share a common background. Is there a way I can do this without having to add an UIImageView control to each View?
I have tried this below but it causes my application crash with a stack overflow error:
-(void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"MyBackgroundImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:imageView];
[self.view sendSubviewToBack:imageView];
}
Is there a better way to do this? What is the best way to handle this kind of theming in iOS applications?
Subclass UIViewController and override -viewDidLoad to create your image and set it as the background of the view. Now make the view controllers that require this background image subclasses of your custom view controller instead of UIViewController.
In your ViewDidLoad:
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 50)];
imgView.image = [UIImage imageNamed:#"image.png"];
[self.view addSubview: imgView];
[self.view sendSubviewToBack:imageView];
Should do the trick.
It turns out that the code I posted above is working perfectly. My solution was the same as Mark Adams' suggestion: Subclass UIViewController, override -viewDidLoad, create and set the imageView, and use the new subclass as my viewController.
I think I may have inadvertently set my new subclass to an incorrect control in Interface Builder which caused my initial solution not to work correctly.

How to set UITableView background to image?

I've noticed that a number of apps have a custom background image for UITableView (or have set the background to a color.)
I can't find any API in the UITableView class to affect this, and my attempts in interface builder have failed (trying to set the color of the UIView)
Any suggestions? I've seen a third party app with pictures, so I know it can be done.
I just ran into the same question myself, but I found a way to do it that TomSwift touched on. As he mentioned, UITableView has a backgroundView property that is intended to do just what you're looking for. If you want to set the background color, you should instead use the backgroundColor property, as others have said. So:
// Set an image as the background of a UITableView called 'tableView'
UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"MyImage.png"]];
[tableView setBackgroundView:bg];
or:
// Set a solid color as the background of a UITableView called 'tableView'
// In this case I chose red
[tableView setBackgroundColor:[UIColor redColor]];
add this line in you code where you want to set background color for your image
[YourView(or imageView) setbackgroundColor:[UIColor
groupTableViewBackgroundColor]];
Thanks
UIImageView *image = [[UIImageView alloc] initWithImage:
[UIImage imagenamed:#"no_image.png"]];
[self.view addSubview:image];
UITableView *tableview = [[UITableView alloc] init];
[image addSubview:tableview];
tableview.backgroundcolor = [UIColor clearColor];
try this. it works for me.
CALayer *background = [CALayer layer];
background.zPosition = -1;
background.frame = self.view.frame;
background.contents = [[UIImage imageNamed:#"background.jpg"] CGImage];
[tableView.layer addSublayer:background];
you need to add an image view and set the table background color to clear color see this link for tutorial
Set the backgroundView property of the tableView. You can set it to a view where you have set a custom backgroundColor, or you can set it to a UIImageView where you've set an image.
alternative solution of GhostRider
No coding
Make UiImageView by the interface builder,
put your image in resources.
set it in UIImageView Image property by the use of Inspector.
select table view and set it background color to clear(for this make opacity to 0 by using attribute inspector).
This is how I did it. Probably not the best way, but works well enough.
UIImage *bg = [UIImage imageNamed:#"bg"];
[bg drawInRect:self.view.window.frame];
UIImageView *bgView = [[UIImageView alloc] initWithFrame:self.view.window.frame];
bgView.image = bg;
activityTable.backgroundView = bgView; //activityTable = UITableView

Replicating Style of Copy/Paste Pop-out

How would I replicate the style of the Copy/Paste pop-out so I could put custom buttons inside it.
I don't think it's a public class so how else would I do this?
Just instantiate a view in the desired size, add a UIImageView with the backround image on it and add your buttons
UIView *overlayView = [[UIView alloc] initWithFrame:aRect];
UIImageView *bgView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:thePath]];
[overlayView addSubview:bgView];
[overayView addSubview:aButton];
[aButton release];
[bgView release];
untested
This code could be executed by a UILongPressGestureRecognizer
I was able to figure it out.
Check out UIMenuController and UIMenuItem.
If you need more help, watch this Youtube Video for a tutorial.
http://www.youtube.com/watch?v=x1-Ty0FvFK0

UIViewController with background image

How can I insert into my UIViewController an image from the resources as background image?
thanks in advance!
Add it UIViewController's view, somewhat like this:
- (void) viewDidLoad
{
UIImage *background = [UIImage imageNamed: #"background.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: background];
[self.view addSubview: imageView];
[imageView release];
[super viewDidLoad];
}
UIViewControllers don't have background images. Only views themselves have visual attributes.
UIView does not have a background image property. To display a background image, you usually simply put a UIImageView displaying the image in the view hierarchy so that it appears visually behind all other views. This can be done programmatically or in Interface Builder.

Why does UITableViewCell backgroundView blur an image

I am subclassing a UITableViewCell and in the init function I am doing the following:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"packagelistcell_background.png"]];
[imageView setFrame:CGRectMake(0, 0, 300, 81)];
self.backgroundView = imageView;
[imageView release];
When that displays on the screen, the image appears slightly blurred. For testing, I placed a straight UIImageView above the table view and the image looks perfect and crisp. The table view is a grouped table view. Do you know why it is blurring the image?
I think the cell has been resized and so is the backgroundView.
Try
imageView.autoresizingMask = UIViewAutoresizingNone;
Try shifting the imageView by 0.5 pixels.
Unfortunately #freytag's solution didn't work for me.
To solve this problem I had to add the UIImageView to a UIView container and set this last as background view of my Custom Cell instead of setting directly the UIImageView:
UIImageView *imgView = [[UIImageView alloc] initWithImage:#"cellBackground.png"];
UIView *backgrContainer = [[UIView alloc] initWithFrame:self.backgroundView.frame];
[backgrContainer addSubview:imgView];
[self setBackgroundView:backgrContainer];// instead of [self setBackgroundView:imgView];
[backgrContainer release];
[imgView release];
For some reason setting directly the UIImageView makes it blurry (even if no resize ocurrs). Setting autoresizingMask = None or contentMode <> ScaleToFill for the UIImageView did not fix my problem.
Also beware of the UITableView style, since using a Grouped Style will result in less width for the cell's background view, and this can also resize the image.
( Retina device/simulator doesn't seem to have this problems about blurry image in the cell backgroundVIew)