Xamarin.IOS将Splitview添加到选项卡式应用程序

时间:2014-12-08 19:08:17

标签: c# ios objective-c xamarin.ios xamarin

我正在使用Xamarin.IOS编写应用程序。我的主视图有一个标签布局,所有的UIElements都在故事板中设置。现在,我需要做什么,因为我想实现IAd, 我希望我的应用程序是一个拆分视图,IAd视图控制器作为详细信息视图,我通常的TabbedLayout作为主视图。

所以,是的,我的确切问题是:

由于我的所有布局都是从Storyboard加载的:我如何将通常加载的View实例传递给我的SplitViewController?

你能给出的任何提示都很有用,我的工作确实需要这个提示!!

提供一些代码:

我正在尝试在AppDelegate.cs中加载我的初始View(这是TabBarController):

        SplitViewContoller splitView = new SplitViewContoller();
        IADViewController iAdVC = new IADViewController (splitView);
        Console.WriteLine ("Root" + Window.RootViewController);
        Window.RootViewController = iAdVC;

这样工作正常,除了通常出现的视图没有加载... IAD显示但屏幕的其余部分是一个空的TabBarController。

这是我的SplitViewController:

public class SplitViewContoller : UISplitViewController
{
    UIViewController masterView, detailView;

    public SplitViewContoller () : base()
    {
        // create our master and detail views
        masterView = new TabBarController();
        detailView = new IADViewController (new TabBarController());
        // create an array of controllers from them and then
        // assign it to the controllers property
        ViewControllers = new UIViewController[]
        { masterView, detailView }; // order is important
    }

    public override bool ShouldAutorotateToInterfaceOrientation
    (UIInterfaceOrientation toInterfaceOrientation)
    {
        return true;
    }
}

我想要位于屏幕底部的视图: (有关详细信息,请参阅Monotouch.Dialog and iAds

public partial class IADViewController : UIViewController
{
    private UIViewController _anyVC;
    private MonoTouch.iAd.ADBannerView _ad;

    public IADViewController (UIViewController anyVC)
    {
        _anyVC = anyVC;
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        View.AddSubview (_anyVC.View);

        Version version = new Version (MonoTouch.Constants.Version);
        if (version > new Version (6,0)) 
        {


            try {
                _ad = new MonoTouch.iAd.ADBannerView (MonoTouch.iAd.ADAdType.Banner);
                _ad.Hidden = true;
                _ad.FailedToReceiveAd += HandleFailedToReceiveAd;
                _ad.AdLoaded += HandleAdLoaded;
                View.BackgroundColor = UIColor.Clear;
                _anyVC.View.Frame = View.Bounds;
                View.AddSubview (_ad);
            } catch {
            }
        } else {
            Resize ();
        }
    }

    public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
    {
        base.DidRotate (fromInterfaceOrientation);
        Resize ();
    }

    public override void ViewDidAppear (bool animated)
    {
        base.ViewDidAppear (animated);
        Resize ();
    }

    void Resize ()
    {

        UIView.Animate (.25,
            () => {
                if (_ad !=null && _ad.Hidden == false) {
                    _anyVC.View.Frame = new RectangleF (0, 0, this.View.Bounds.Width, this.View.Bounds.Height - _ad.Frame.Height);
                } else {
                    _anyVC.View.Frame = View.Bounds;
                }
            });
        if(_ad!=null)
            _ad.Frame = new RectangleF (0, _anyVC.View.Bounds.Height, this.View.Bounds.Width, _ad.Frame.Height);
    }

    void HandleAdLoaded (object sender, EventArgs e)
    {
        if (_ad == null)
            return;
        _ad.Hidden = false;
        Resize ();
    }

    void HandleFailedToReceiveAd (object sender, AdErrorEventArgs e)
    {
        if (_ad == null)
            return;
        _ad.Hidden = true;
        Resize ();
    }
}

1 个答案:

答案 0 :(得分:0)

如果有其他人需要这个,我现在用以下几行代码解决了这个问题。

            Storyboard = UIStoryboard.FromName ("MainStoryboard", null);

            initialViewController = Storyboard.InstantiateInitialViewController () as UITabBarController;
            // If you have defined a root view controller, set it here:

            IADViewController iAdVc = new IADViewController (initialViewController);

            Window.RootViewController = iAdVc;


            // make the window visible
            Window.MakeKeyAndVisible ();