0

I faced strange endless loop in some cases using setBackgroundView for UITableView appearance. Here is appearance initialization:

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"bg"]];
[[UITableView appearance] setBackgroundView:iv];

I have a controller:

@interface MyController : UITableViewController

that has only init method and viewWill*/viewDid* delegates with simple logging. Nothing else. In such case I'm getting endless messages about layouting subviews:

2013-06-05 21:23:45.054 MyApp[16700:c07] init
2013-06-05 21:23:45.056 MyApp[16700:c07] viewDidLoad
2013-06-05 21:23:45.057 MyApp[16700:c07] viewWillAppear
2013-06-05 21:23:46.059 MyApp[16700:c07] viewWillLayoutSubviews
2013-06-05 21:23:47.061 MyApp[16700:c07] viewDidLayoutSubviews
2013-06-05 21:23:48.064 MyApp[16700:c07] viewWillLayoutSubviews
2013-06-05 21:23:49.066 MyApp[16700:c07] viewDidLayoutSubviews
2013-06-05 21:23:50.067 MyApp[16700:c07] viewWillLayoutSubviews
2013-06-05 21:23:51.069 MyApp[16700:c07] viewDidLayoutSubviews
2013-06-05 21:23:52.070 MyApp[16700:c07] viewWillLayoutSubviews

If I'll switch to:

@interface MyController : UIViewController

everything is going well.

Is this expected behavior (and I didn't understood UIAppearance clearly) or this is broken functionality?

UPD: appearance initialized in AppDelegate.

UPD2: endless loop happens only on table view controllers that are pushed from another one

Oleksandr
  • 419
  • 3
  • 14
  • When / where are you modifying the appearance? – Wain Jun 05 '13 at 19:15
  • @Wain Looks like appdelegate, to me. – Undo Jun 05 '13 at 19:15
  • Yes, I do this in appdelegate. – Oleksandr Jun 05 '13 at 19:19
  • Hmm.... I pasted your code into my app delegate, and I didn't get any infinite loop. The appearance was changed correctly. – rdelmar Jun 05 '13 at 19:33
  • 1
    I do get the endless loop though if I try to push from the first table view controller to another one. – rdelmar Jun 05 '13 at 19:45
  • IMO this seems like a bug in `UITableViewController` if this isn't happening with simple `UITableView` instances in `UIViewController` – Ryan Poolos Jun 05 '13 at 19:51
  • @RyanPoolos, I agree, this looks like a bug. Easy to get around though -- just set the backgroundView in the viewDidLoad method of your tableViewController(s). – rdelmar Jun 05 '13 at 19:55
  • @rdelmar I started to share my code, but you was faster. :) Yes, I also have a loop only with pushed table view controller. I'll update my question with this note. – Oleksandr Jun 05 '13 at 20:02
  • 1
    I noticed that this has to do with the animation of the transition. If I switch to a modal transition and uncheck the "Animates" box, I don't get the infinite loop (I do get it with a modal using any of the transition styles except flip horizontal). – rdelmar Jun 05 '13 at 20:10

2 Answers2

0

According to this answer UITableView doesn't support appearance customization using setBackgroundView. So this isn't a bug - it simply unsupported yet.

Community
  • 1
  • 1
Oleksandr
  • 419
  • 3
  • 14
0

According to Apple documentation, UITableView has a background property.

self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"BackgroundImage.png"]];

Setting an image as the background of a UITableView is possible, albeit slight convoluted. There are four steps:

1) Set the backgroundColor property of your tableView to clearColor, so that the background image is visible.

[myTableView setBackgroundColor:[UIColor clearColor]];

2) Create an instance of a UIImageView and set its image property to the image that you want to appear behind the table.

UIImageView *tableBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImage"]];

3) Set the UIImageView’s frame property so that it’s the same size as that of the tableView:

[tableBackgroundView setFrame: myTableView.frame];

4) Update the tableView’s backgroundImage property to point to your new UIImageView object:

[myTableView setBackgroundView:tableBackgroundView];
sangony
  • 11,636
  • 4
  • 39
  • 55
  • I used same approach earlier, before I have switched to iOS 5 UIAppearance. Once I switched, I faced with setBackgorundView issues, using UIAppearance. So my question concerns UIAppearance. But seems there is no right solution for UIAppearance, so your answer could be used as workaround. Thanks. – Oleksandr Jun 05 '13 at 22:21
  • If I understand you correctly, you are looking to have a custom background image for your table. If so, why are you using UIAppearance to accomplish this? – sangony Jun 05 '13 at 22:23
  • I have no requirements for my app about UIAppearance approach, I simply decided to try newer API, since it simplier and doesn't require straight subclassing of everything. – Oleksandr Jun 06 '13 at 05:43