1

environment: creating an iPad application using Monotouch and the Monotouch.Dialog library.

I've been trying to set the background color on a DialogViewController to no avail. I have multiple views in my application being loaded an unloaded. For non of them I manage to set the background color.

What I have tried so far:

  • Set the background color on the main window of my application --> works fine.
  • Create a simple UIView, give it a size, set the background color and load it into the window --> works fine.

But as soon as I load a DialogViewController (with an associated view) the background color is always gray. The DialogViewController is used from the Monotouch.Dialog framework.

I'm pushing the DialogViewController onto a navigation controller to show a set of buttons laid out in a table view.

I must be missing out on something fundamental ! I have been looking through the Monotouch.Dialog code and tried a couple of other things, but nothing fixed my problem so far.

Any help highly appreciated.

boris

poupou
  • 43,413
  • 6
  • 77
  • 174
Boris Rogge
  • 35
  • 1
  • 5

4 Answers4

14

You actually need to set the background view to null. This is the view that is behind a table view, such as the grouped one in MonoTouch.Dialog

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

Here is what a subclass for this might look like:

 using System;
 using System.Drawing;
 using System.IO;
 using MonoTouch.Foundation;
 using MonoTouch.CoreGraphics;
 using MonoTouch.Dialog;
 using MonoTouch.UIKit;

 namespace MyNameSpace{

     public class MySpecialDialogViewController : DialogViewController {

       public MySpecialDialogViewController (UITableViewStyle style, RootElement root) 
              : base (style, root) 
         {
         }

        public override void LoadView ()
        {
            base.LoadView ();
            TableView.BackgroundView = null;
             TableView.BackgroundColor = UIColor.Black;
        }
     }

  }
Eric Welander
  • 560
  • 4
  • 11
  • +1 Miguel's solution did used to work, I can only assume a recent iOS/MT change means you now need to set `BackgroundView` to `null`. – James Nov 04 '12 at 17:05
8

This is described in the section "Customizing the DialogViewController" in the MonoTouch.Dialog documentation.

You need to subclass DialogViewController, like this:

public class ColoredViewController : DialogViewController {
    [...]

    public override LoadView ()
    {
        base.LoadView ();
        TableView.BackgroundColor = UIColor.Clear;
        ParentViewController.View.BackgroundColor = UIColor.Red;
    }
}
miguel.de.icaza
  • 32,654
  • 6
  • 58
  • 76
  • 1
    I've implemented your code. But got the same problem the TableDialog is Gray all the Time. Can you explain your Solution more detailed or tell me another way how can i fix it? In your MonoTouch.Dialog sample on Git i got the same Problem. With the iPhone simulater everithing is fine, but when i start the applikation with the ipad simulater th background is gray. – Shabby Jul 16 '12 at 13:40
  • 1
    @Shabby see Eric's answer below, I think there has been a recent change which now means you need to set the BackgroundView property to null. Miguel could you confirm this? – James Nov 27 '12 at 15:31
2

Yes Eric's solution works now. I modified his below if you would like to use an image instead of a color.

        public override void LoadView () 
    {
        base.LoadView ();
        this.TableView.BackgroundView = null;
        //this.TableView.BackgroundColor = UIColor.Black;
        var background = UIImage.FromFile ("Images/down.png");
        this.TableView.BackgroundColor = UIColor.FromPatternImage(background);
    }
darrellbooker
  • 188
  • 1
  • 7
1

I find the pattern gets duplicated when using the other solutions and so setting the backgroundview is more preferable for me like so:

    public override void LoadView ()
    {
        base.LoadView ();
        UIImage tickImage = UIImage.FromBundle ("1.jpg");
        UIImageView backgroundImageView = new UIImageView (this.View.Bounds);
        backgroundImageView.Image = tickImage;
        backgroundImageView.ContentMode = UIViewContentMode.BottomLeft;  //your preference
        TableView.BackgroundView = backgroundImageView;
    }
fractal
  • 1,649
  • 17
  • 31