我如何添加一个对话框视图控制器作为一个子视图到一个UIView,反之亦然?(How can I ad

2019-07-29 11:03发布

我已经看过网上有一段时间寻找任何资源对这个议题,并拿出了什么,解决我的困境。

我有一个对话视图控制器和它的根是简单地显示类似iPhone的音乐歌曲滚动视图是如何布局的字符串列表。 我需要的是位于屏幕的顶部和下面的滚动DVC子视图。 我需要顶视图中始终处于视图,同时用户可以通过根元素滚动,因为顶视图将举行统计。

我曾尝试添加一个子视图,但它只是重叠它下面的DVC,我一直没能想出一个方法来添加一个DVC作为一个子视图到一个UIView。

任何帮助将非常感激。

Answer 1:

所需要的以实现此是承载2个视图控制器单根视图控制器。 一个子视图包含在窗口顶部的统计信息。 底部子视图包含保持所述对话视图的导航控制器。

using System;
using System.Collections.Generic;
using System.Linq;

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

namespace delete201205203
{
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        UIWindow window;
        MyUIViewController _mvc;

        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            window = new UIWindow (UIScreen.MainScreen.Bounds);

            _mvc = new MyUIViewController ();

            window.RootViewController = _mvc;
            window.MakeKeyAndVisible ();

            return true;
        }
    }

    public class MyUIViewController : UIViewController
    {
        MyDialogViewController _dvc;
        UINavigationController _nav;
        StatisticsViewController _statistics;

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

            var root = new RootElement ("Root") {
                new Section ("Section") {
                    new EntryElement ("caption", "placeholder", ""),
                    new RootElement ("Root 2") {
                        new Section ("Section") {
                            new EntryElement ("caption", "placeholder", ""),
                            new StringElement ("Back", () => {
                                _nav.PopViewControllerAnimated (true);  
                            })
                        }
                    }
                }
            };

            _dvc = new MyDialogViewController (root);
            _nav = new UINavigationController (_dvc);
            _nav.SetNavigationBarHidden (true, false);
            _nav.View.Frame = new RectangleF (0, 70f,
                                              this.View.Bounds.Width, 
                                              this.View.Bounds.Height -70f);

            _statistics = new StatisticsViewController ();
            _statistics.View.Frame = new RectangleF (0, 0,
                                              this.View.Bounds.Width, 
                                              70f);

            this.AddChildViewController (_nav);
            this.View.AddSubview (_nav.View);

            this.AddChildViewController (_statistics);
            this.View.AddSubview (_statistics.View);
        }

        public override void ViewWillLayoutSubviews ()
        {
            base.ViewWillLayoutSubviews ();
            _nav.View.Frame = new RectangleF (0, 70f,
                                              this.View.Bounds.Width, 
                                              this.View.Bounds.Height -70f);

            _statistics.View.Frame = new RectangleF (0, 0,
                                              this.View.Bounds.Width, 
                                              70f);
        }

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

    public class StatisticsViewController : UIViewController
    {
        UILabel _label;
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            this.View.BackgroundColor = UIColor.White;

            _label = new UILabel (new RectangleF (this.View.Bounds.Width * .5f - 50f,
                                                  this.View.Bounds.Height * .5f -10f,
                                                  100f, 20f));
            _label.AutoresizingMask = UIViewAutoresizing.FlexibleMargins;

            _label.Text = "statistics";
            this.View.AddSubview (_label);

        }

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

    // This overrde is needed to ensure the pop view animation  
    // works correctly in landscape mode
    public class MyDialogViewController : DialogViewController
    {
        public MyDialogViewController (RootElement root) : base (root) {}
        public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
        {
            return true;
        }   
    }
}


文章来源: How can I add a Dialog View Controller as a Subview to a UIView or Vice Versa?