Arnold.Krumins
Arnold.Krumins

Reputation: 1085

How do I style RootElement

I need a way to style Monotouch Dialogs RootElement. I need to change the background and font color.

I'm have created a custom RootElement as below

public class ActivityRootElement : RootElement
{
    public ActivityRootElement (string caption) : base (caption)
    {

    }

    public ActivityRootElement(string caption, Func<RootElement, UIViewController> createOnSelected) : base (caption, createOnSelected)
    {
    }

    public ActivityRootElement(string caption, int section, int element) : base (caption, section, element)
    {
    }

    public ActivityRootElement(string caption, Group group) : base (caption, group)
    {
    }



    public override UITableViewCell GetCell (UITableView tv)
    {
        tv.BackgroundColor = Settings.RootBackgroundColour;
        return base.GetCell (tv);
    }

    protected override void PrepareDialogViewController(UIViewController dvc)
    {
        dvc.View.BackgroundColor =  Settings.RootBackgroundColour;
        base.PrepareDialogViewController(dvc);
    }

}

I am then calling the custom root element as below passing in a custom DialogController

    section.Add (new ActivityRootElement(activity.Name, (RootElement e) => {
                return new ActivityHistoryDialogViewController (e,true);
            }));

The root Element style is not been applied. Any help would be apprciated!!

Upvotes: 1

Views: 558

Answers (2)

Christian Moore
Christian Moore

Reputation: 11

In order to get this to work, I had to override the MakeViewController method and cast the UIViewController that it normally returns to a UITableViewController, then make my edits.

protected override UIViewController MakeViewController()
{
    var vc = (UITableViewController) base.MakeViewController();

    vc.TableView.BackgroundView = null;
    vc.View.BackgroundColor = UIColor.Red; //or whatever color you like
    return vc;
}

Upvotes: 0

therealjohn
therealjohn

Reputation: 2398

If you want the color to be the only thing you see in the TableViewController, you need to set the BackgrounView to null. There is a view overtop to apply the styling, which will hide the color you are looking for.

Try this:

public override UITableViewCell GetCell (UITableView tv)
{
    tv.BackgroundColor = Settings.RootBackgroundColour;
    tv.BackgroundView = null;
    return base.GetCell (tv);
}

protected override void PrepareDialogViewController(UIViewController dvc)
{
    dvc.TableView.BackgroundColor =  Settings.RootBackgroundColour;
    dvc.TableView.BackgroundView = null;
    base.PrepareDialogViewController(dvc);
}

Upvotes: 1

Related Questions