Xamarin.IOS 将拆分视图添加到选项卡式应用程序

本文关键字:选项 应用程序 添加 IOS 拆分 视图 Xamarin | 更新日期: 2023-09-27 18:33:30

我正在Xamarin.IOS上编程一个应用程序。我的主视图有一个选项卡式布局,所有 UIE 都在故事板中设置。现在,我需要做的是,因为我想实施IAds,我希望我的应用程序是拆分视图,IAd 视图控制器作为详细信息视图,我通常的选项卡式布局作为主视图。

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

由于我的所有布局都是从情节提要加载的:如何将通常加载的视图实例传递给我的 SplitView控制器?

您可以提供的任何提示都是有帮助的,我真的需要这个来工作!!

要提供一些代码:

我正在尝试在应用程序委托中加载我的初始视图(即选项卡控制器.cs:

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

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

这是我的 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 和 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 ();
    }
}

Xamarin.IOS 将拆分视图添加到选项卡式应用程序

如果其他人需要这个,我现在用下面的代码行解决了它。

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