2

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:

enter image description here

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;
 }
K.Honda
  • 3,106
  • 5
  • 26
  • 37

2 Answers2

2

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.

Deepak Danduprolu
  • 44,595
  • 12
  • 101
  • 105
0

What you have should probably work out. Still try this :
Add

tblSimpleTable.backgroundView = nil;  

in viewDidLoad

Nitish
  • 13,845
  • 28
  • 135
  • 263
  • @Nitish: Unfortunately, that doesn't work. I've seen some posts saying that they need to put the image in IB - **send to back**. However, it doesn't let me do that as the **send to back** option in IB is disabled. Do you know why? Thanks. – K.Honda Jun 22 '11 at 08:54
  • I don't think there is any need for this. Do you want to place the image behind the tablecells or behind the complete view? – Nitish Jun 22 '11 at 09:00
  • @Nitish: I want to place the image behind the tablecells and button. So the image is the background and the tablecells and button is above the image. Do you know how? Thanks. – K.Honda Jun 22 '11 at 09:07
  • If you want to place image behind the cells, then you should also clear background color of cell and its subviews. The same way you did for tableView. Try it. – Nitish Jun 22 '11 at 09:09
  • @Nitish: Yes, I've tried it but it still doesn't display the background image. :/. Is there another way to show it? Thanks. – K.Honda Jun 22 '11 at 09:16
  • And you should prefer create the image in custom cell. When you have created the image then just add the image in cell as subview as you did for username and password. But do remember that you add the image before adding the two textviews. – Nitish Jun 22 '11 at 09:18
  • And remove the image from IB. – Nitish Jun 22 '11 at 09:19