UITableView not showing background image - iphone

I have a uitableview with 2 cells (custom tableviewcells) and a button for login. I made them programmatically. Now I want to put a background image behind the 2 custom cells and the button. I went into IB and put the image view on top of the tableview and then put a picture on it which I want as the background. Then I went into viewDidLoad and put this bit of code tblSimpleTable.backgroundColor = [UIColor clearColor]; to clear the tableviews background colour.
It now looks like this:
But the background does not appear. Does anyone know how to fix this?
Thanks.
viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
self.dataSourceArray = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
#"UITextField", kSectionTitleKey,
#"TextFieldController.m: textFieldNormal", kSourceKey,
self.textFieldNormal, kViewKey,
nil],
nil];
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
loginButton.frame = CGRectMake(60,140,200,38); // position in the cell and set the size of the button
[loginButton setTitle:#"Login" forState:UIControlStateNormal];
// add targets and actions
[loginButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
// add to a view
[self.view addSubview:loginButton];
tblSimpleTable.backgroundColor = [UIColor clearColor];
//tblSimpleTable.backgroundView = nil;
//UIImageView *imageView = [[UIImageView alloc] initWithImage:#"Background-Red.png"];
//imageView.frame = tblSimpleTable.frame;
//tblSimpleTable.backgroundView = imageView;
self.title = NSLocalizedString(#"TextFieldTitle", #"");
// we aren't editing any fields yet, it will be in edit when the user touches an edit field
self.editing = NO;
}

Why don't you use backgroundView property of the UITableView object?
UIImageView * imageView = [[UIImageView alloc] initWithImage:yourBackgroundImage];
imageView.frame = tblSimpleTable.frame;
tblSimpleTable.backgroundView = imageView;
Of course, backgroundView will be resized if you don't set the frame so skipping the second line should also not be a problem.

What you have should probably work out. Still try this :
Add
tblSimpleTable.backgroundView = nil;
in viewDidLoad

Related

How to make expandable/collapsable ImageView

I have many textViews on top of a scrollView and every textView has a custom button over it so that when user click that textView it should expand and when it click back then it should collapse to previous position.
what I'm thinking of doing is hide the smallTextView and show the expandedTextView when button is pressed and when the button is pressed i want to hide expandedtextView n show the smallTextView. but I don't know how I should do it. any help will be appreciated.
Here is my code:
-
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = #"Demo";
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
for (int i=0;i<[appDelegate.serverResponseArray count];i++)
{
self.expandTextView = [[UITextView alloc] init];
[self.expandTextView setFrame:CGRectMake(8.0f, i*50.0f+10.0f, 270.0f, 40.0f)];
[self.expandTextView setBackgroundColor:[UIColor grayColor]];
[self.expandTextView setFont:[UIFont fontWithName:#"helvetica" size:12]];
[self.expandTextView setText:#"Welcome!!!"];
[self.expandTextView setTextColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1]];
[self.expandTextView setUserInteractionEnabled:NO];
[self.scrollView addSubview:self.expandTextView];
self.expandTextView = nil;
self.expandButton = [[UIButton alloc]initWithFrame:CGRectMake(8.0f, i*50.0f+1.0f, 270.0f, 60.0f)];
[self.expandButton setBackgroundColor:[UIColor clearColor]];
[self.expandButton addTarget:self action:#selector(textButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.expandButton.tag = i;
[self.scrollView addSubview:self.expandButton];
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(278.0f, i*50.0f+10.0f, 14.0f, 40.0f)];
[imageView setBackgroundColor:[UIColor grayColor]];
[imageView setImage:[UIImage imageNamed:#"arrow.png"]];
[self.scrollView addSubview:imageView];
imageView = nil;
}
float maxHeight = 0;
for(UIView *v in [self.scrollView subviews])
{
if(v.frame.origin.x + v.frame.size.height > maxHeight)
maxHeight = v.frame.origin.x + v.frame.size.height;
}
self.scrollView.contentSize = CGSizeMake(_scrollView.frame.size.width, maxHeight+2570);
}
-(IBAction)textButtonClicked:(id)sender
{
NSLog(#"%#",sender);
}
and how do I know which button is getting pressed.
To know which button press you have to assign tag value to your button then
-(IBAction)textButtonClicked:(id)sender
{
UIButton *btn=(UIButton*)sender;
NSLog(#"Tag of button = %d",btn.tag);
}
And i suggest you to change frame of your existing imageView instead of using other image view(hide and show)
First of all , I'd suggest you to use UITableView instead of UIImageView and then implement Expandable and Collapsible UITableViewCell.
There are lots of Tutorial to implement this Functionality. Simply Google it and you will have plenty of solutions for that.
For Example : Expandable/Collapsible Table For iOS
Check this Sample Code provided by Apple : Table View Animations and Gestures.
The Best way is Take UITableView and Put your images on Cell of UITableView and change size of cell when your click on it, i give you simple example for how to create expandable UITableViewCell.
http://www.roostersoftstudios.com/2011/04/14/iphone-uitableview-with-animated-expanding-cells/

Is there anyway to create a UITableView with one universal footer?

I'm trying to create a UITableView with multiple sections. Each section has it's own header, but I am trying to make a universal footer for the entire table view that stays in one position...
Is this logic possible using the UITableViewDelegate methods? Or should I create a custom view and just try and add it as a subview to my table view? The footer I currently have contains a UIButton.
If anyone has some sample code that would be great.
Edit: This question is not the same as the one referenced. I am trying to make a universal footer that floats above the UITableView. The other question does not specify the location of the footer, only that a footer is desired.
read about UITableView's tableFooterView property.
And now some code: (using ARC)
UILabel *footer = [UILabel alloc] init];
footer.text = #"Some text" ;
footer.backgroundColor = [UIColor greenColor];
self.tableView.tableFooterView = footer;
Now at the bottom of entire tableview there is a UILabel that is green with some text.
I ended up creating a subview and adding my button to that view. I then made that view my "footer".
Here is the code that gave me the desired results.
- (void)viewDidLoad
{
[super viewDidLoad];
//self.tableView.delegate = self;
//self.tableView.dataSource = self;
//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
self.view = replacementView;
[self.view addSubview:self.tableView];
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 370, 320, 45)];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.userInteractionEnabled = YES;
//the button should be as big as a table view cell
//width of the button can be set but the width of the view it is added to will always match the width of the tableView
[button setFrame:CGRectMake(60, 0, 200, 45)];
//set title, font size and font color
[button setTitle:#"Build" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(buildThenSegue)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
footerView.userInteractionEnabled = YES;
[self.view addSubview:footerView];
self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
Take note that this is in a subclass of UITableViewController.
I referenced this answer to another question: https://stackoverflow.com/a/9084267/1091868

How to navigate into other screen while clicking a image in iPhone

I am very to new phone i want to display four images in one line like tabbar and also if the user clicks any of the image i want to show another screen in iPhone.
can any one tell me how can i do that and also if possible please tell me in which files i have to write the code to achieve this requirement.
Try This
In ViewDidLoad
CGRect myImageRect = CGRectMake(10.0f, 5.0f, 80.0f, 95.0f);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImageRect];
[imageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
[imageView.layer setBorderWidth: 2.0];
imageView.opaque = YES; // explicitly opaque for performance
[self.view addSubview:imageView];
UIButton *newbutton1 = [[UIButton alloc] init];
[newbutton1 setFrame:myImageRect];
[newbutton1 addTarget:self action:#selector(myMethod:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:newbutton1];
//Method on Button Click
-(void)myMethod:(id)sender {
//Button is pressed
}
Make some xib file and build your UI.
You need to set other screens as IBOutlets and after click on button perform action to switch view.
You can set image background for UIButton.
Try, it isn't so big problem.
UIButton with images tutorial
Is a great tutorial for creating clickable images.
arrange 4 images manually in ur screen
eg
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 100, 100)];
myImageView.image = [UIImage imageNamedWith:#"ur image.png"];
UIGestureRecognizer *recognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(showtheUI:)];
[myImageView addGestureRecognizer:recognizer1];
recognizer1.delegate = self;
[recognizer1 release];
[self.view addSubView:myImageView ];
}
//this method will trigger when u tapped the 1st image
-(void)showtheUI(UIImageViee *)sender{
}
create 2nd imageView like
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(120, 10, 100, 100)]; etc
or use a for loop to create 4 image Views. set frame according to the i value
you need to create the custom table or more things to use for the creating a grid type of image
for that you need to use following things to use
TableView
Custom UITableViewCell
Array of images
HJCache for asynchronous image loading.
You can add buttons on your images and handle click events of buttons to Navigate to other screens

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.

how to display an image in the navigation bar of an iPhone application?

how to display an image in the navigation bar of an iPhone application? (say, right after the title)
Here's how to have the image centered in the NavBar.
UIImage *image = [UIImage imageNamed: #"NavBarImage.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: image];
self.navigationItem.titleView = imageView;
[imageView release];
This code is actually contained with the Apple source for the NavBar and can be found at the iPhone Developer Center at Apple.
The source shows you various ways to manipulate both the StatusBar & NavBar.
I haven't tested this but as UINavigationBar is a view you can add subViews to it.
UIImage* myImage = [UIImage imageNamed:#"Myimage.png"];
UIImageView* myImageView = [[UIImageView alloc] initWithImage:myImage];
myImageView.frame.origin.x = 30.0; // You will have to find suitable values for the origin
myImageView.frame.origin.y = 5.0;
[myTabbar addSubView:myImageView];
[myImageView release];
You can use things like the backItem property to calculate the position of your image view.
If you want the image at the right of the nav bar, you can define it as a custom button with no action when presed, like this
UIButton* fakeButton = (UIButton *) [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]];
UIBarButtonItem *fakeButtonItem = [[UIBarButtonItem alloc] initWithCustomView:fakeButton];
self.navigationItem.rightBarButtonItem = fakeButtonItem;
[fakeButtonItem release];
[fakeButton release];
Simply Place that code in - (void)viewWillAppear:(BOOL)animated; so it'll work fine
and add one image having size 320x40 named Top Bar
UIImage *image = [UIImage imageNamed: #"TopBar.png"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
the navigation bar has a property called title view - set this to the image you like. Since the titleView overwrites the title of the nav bar you have to include the desired title in the image file. Still set the title to what you want so it appears on the back button when you push a view Controller
I encountered the same problem.Found out the best solution
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"background_image.png"] forBarMetrics:UIBarMetricsDefault];
Hope this would help....
Just write your own navigation bar. Therefore you have to disable the Navigation Bar fist:
Disable the top bar in the interface builder by selecting your Navigation Controller in
your Storyboard: Attributes Inspector -> Simulated Metrics -> Top Bar: select None
Afterwards you can add any HeaderView you like...
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, sFrame.size.width, 100)];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"header_image.png"]];
self.headerView.backgroundColor = background;
// ...add buttons and labels
[self.view addSubview:headerView];