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.
Related
I have a few buttons that each load a different image a UIScrollView, you can zoom in on these images. What happens is that after selecting a new images the previous one is still there in the scrollview. How do I clean up the scroll view before opening a new image in there?
This is the code so far:
- (UIView *) viewForZoomingInScrollView: (UIScrollView *)scrollView{
return imgView;
}
-(IBAction)buttonClicked: (UIButton *)sender{
switch (sender.tag) {
case 1:{
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
self.imgView = myImageView;
[myImageView release];
scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 1.0;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imgView];
}
}
}
You keep adding new subviews without removing the previous ones. You could just replace the image content in the existing imageView instead of making a new one.
Replace this:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
self.imgView = myImageView;
[myImageView release];
with
self.imageView.image = [UIImage imageNamed:#"Gath.jpg"];
and remove this
[scrollView addSubview:imgView];
I assume you have linked the imageView to self.imageView elsewhere, such as in the storyboard, or when you create the scrollView.
you can try this,
[myImageView removeFromSuperView];
myImageView=nil;
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
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.
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
I have an UITableview controller without an XIB.
I set the background colour of the tableview by doing this:
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"MmyImage.png"]]];
My image is a gradient image, so eventhough the above code is ok, it redraws the image for each cell when the tableview goes to edit view, and it appears as lines which is not looking elegant.
I would like to set the tableviews background to clear colour and set the superview's colour to the image, so that the tableview transitions smoothly over the superview. However the below code does not work:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];
This makes the background completely white, I dont know why.
Help appreciated..thanks.
Edited:new code but this does not work as well:
UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
imgBg.frame = self.tableView.window.bounds;
[self.tableView.superview addSubview:imgBg];
//[self.view addSubview:imgBg];
//[self.tableView.window sendSubviewToBack:imgBg];
//[self.view bringSubviewToFront:self.tableView];
[self.tableView setBackgroundColor:[UIColor clearColor]];
[imgBg release];
In a custom UITableViewController subclass, self.view will return the same as self.tableView. So what you're doing there is altering the same object. In the end, you get to see the UIWindow. So to get the desired result, you should do this –
[self.view.window setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage.png"]]];
[self.tableView setBackgroundColor:[UIColor clearColor]];
UITableView has a backgroundView property. Use it.
aTableView.backgroundView = [[[UIView alloc] initWithFrame:aTableView.bounds] autorelease];
aTableView.backgroundView.backgroundColor = backgroundColor;
First in your xib file, add a parent view. add table view in that parent view. set parent view's background color to your desired background. then set your tableview's background color to [UIColor clearColor].
Check also, if your table view is a group table style or not? if it is group table style, then the cell have distinct background. Then you need to clear the background of each cell. The best way to do is,
create a blank UIView *bkview = [[UIView alloc] initWithRect:CGRect(0,0)];
set this
as your cells background view.
I hope this will work.
then your second code will be right, except:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:
[UIImage imageNamed:#"myImage.png"]]];
replace this:
UIImageView * imgBg = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:6]];
imgBg.frame = self.view.bounds;
[self.view addSubview:imgBg];
[self.view sendSubviewToBack:imgBg];
Edit
I think:
[self.tableView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.0]]; will helps, that is the only difference now.
I'm creating a horizontal scrolling tableview containing images. I have the swiping functionality working great, and am able to add the images to the scrollView as so in cellForRowAtIndexPath:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
[scrollView setContentSize:CGSizeMake(700, 78)];
UIImage *footImage = [UIImage imageNamed:#"Foot.png"];
UIImageView *footImageView = [[UIImageView alloc] initWithImage:footImage];
footImageView.frame = CGRectMake(0, 0, 80, 78);
footImageView.userInteractionEnabled = YES;
[scrollView addSubview: footImageView];
UIImage *handImage = [UIImage imageNamed:#"Hand.png"];
UIImageView *handImageView = [[UIImageView alloc] initWithImage:handImage];
handImageView.frame = CGRectMake(90, 0, 80, 78);
handImageView.userInteractionEnabled = YES;
[scrollView addSubview: handImageView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(highlightImage)];
[scrollView addGestureRecognizer:tapGesture];
NSLog(#"tapGesture added to scrollView");
[[cell contentView] addSubview:scrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
[pageControl setNumberOfPages:4];
[[cell contentView] addSubview:pageControl];
My tapGesture is registering with the selector, hightlightImage, and just logging that its calling this method correctly.
- (void) highlightImage
{
NSLog(#"tapping tapping");
}
What I REALLY am working for is the ability to highlight/select these images if tapped, and highlight/deselect them if tapped again. I will have another button on screen that will navigate to the next page (irrelevant here).
Am I going down the right path here? Obviously I will populate an NSArray of UIImages and populate the scrollView that way, but just spiking it out for now. If someone could give me some direction or an example of how to make each button separately selectable/de-selectable, that would be GREAT:)
Use UIButton instead of UIImageView.
It's got built-in selected functionality.
get rid of your gestures and add highlightImage as the action to every button.
make your highlightImage into highlightImage:(id)sender
sender should be a button so you can just do
[sender setSelected:YES];