How to set the background color on a DialogViewController (Monotouch.Dialog)

↘锁芯ラ 提交于 2020-01-16 00:47:06

问题


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


回答1:


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;
        }
     }

  }



回答2:


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;
    }
}



回答3:


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);
    }



回答4:


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;
    }


来源:https://stackoverflow.com/questions/7312150/how-to-set-the-background-color-on-a-dialogviewcontroller-monotouch-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!