Here’s a simple technique to set a background image on a UITableView such that the background doesn’t scroll with the table view. Instead on loading the table view directly load it as a subview of a UIImageView. When creating the UIImageView, set its frame to the dimensions of the iPhone, 320X480. Then, after adding the UITableViewController’s view as a subview of the UIImageView, set the table view’s background to be transparent. When you load the image view, you’ll have your table view with the image as it’s background.
Here’s a simple example (table view controller code omitted for brevity):
using System; using System.Collections.Generic; using System.Linq; using MonoTouch.Foundation; using MonoTouch.UIKit; namespace TableBackgroundDemo { public class Application { static void Main (string[] args) { UIApplication.Main (args); } } // The name AppDelegate is referenced in the MainWindow.xib file. public partial class AppDelegate : UIApplicationDelegate { // This method is invoked when the application has loaded its UI and its ready to run public override bool FinishedLaunching (UIApplication app, NSDictionary options) { UIImageView imgView = new UIImageView(UIImage.FromFile("background.jpg")); imgView.Frame = new System.Drawing.RectangleF(0,0,320,480); imgView.UserInteractionEnabled = true; imgView.AddSubview(someTableViewController.View); someTableViewController.TableView.BackgroundColor = UIColor.Clear; window.AddSubview (imgView); window.MakeKeyAndVisible (); return true; } // This method is required in iPhoneOS 3.0 public override void OnActivated (UIApplication application) { } } }
And here’s the final result:
Hi Mike
Thanks for the article, this may be a really stupid question, but I haven’t been able to find any information online… Where exactly do you put the image so that it can be picked up via the UIImage.FromFile method? I tried putting it in the same directory as the code, but no luck. Do you need to register it somewhere?
Thanks
Jacob
Hi Jacob,
You need to set the Build action to “Content”
Where do you set this option? I was looking around everywhere in Mono Develop but could not find it 😦
Hi Mike,
nice solution – but what if I want the background to scroll with the table view?
And it is a grouped one – so background color from pattern doesn’t work.
Manfred
try adding the ImageView as the subview of the UITableView and send it to the back
I did – but this brings lines for empty cells – and nothing (not even seperator lines) where cells have a value.
Interesting Post!