【问题标题】:Monotouch: how to add UIView to DialogViewControllerMonotouch:如何将 UIView 添加到 DialogViewController
【发布时间】:2012-10-25 01:29:52
【问题描述】:

在我的一个屏幕中,我需要将 UIView(带有一些标签和按钮)添加到 DialogViewController。原因是我没有使用 TableView 标头,因为我不希望该视图在表格滚动时滚动。

如果我将自定义视图添加到导航栏,我可以实现这一点,但是我的视图不会收到任何触摸(导航控制器会吃掉它们)。

我还尝试将自定义视图添加到 DialogsViewController 父控制器,虽然它可以工作,但在 LoadView() 中调整 tableview 框架的大小没有任何作用。

还有其他方法可以将自定义视图添加到 DialogViewController 吗?

谢谢。

【问题讨论】:

    标签: uiview xamarin.ios dialogviewcontroller


    【解决方案1】:

    要添加不滚动的标题,您可以创建一个控制器,其视图包含您要添加的其他视图以及 DialogViewController 的视图。例如,以下简单示例将 UILabel 与 DialogViewController 的视图一起添加为附加控制器(在本例中称为 container)的子视图:

       [Register ("AppDelegate")]
        public partial class AppDelegate : UIApplicationDelegate
        {
            UIWindow window;
            MyDialogViewController dvc;
            UIViewController container;
            float labelHeight = 30;
    
            public override bool FinishedLaunching (UIApplication app, NSDictionary options)
            {
                window = new UIWindow (UIScreen.MainScreen.Bounds);
    
                container = new UIViewController ();
    
                container.View.AddSubview (new UILabel (new RectangleF (0, 0, UIScreen.MainScreen.Bounds.Width, labelHeight)){
                    Text = "my label", BackgroundColor = UIColor.Green});
    
                dvc = new MyDialogViewController (labelHeight);
    
                container.View.AddSubview (dvc.TableView);
    
                window.RootViewController = container;
    
                window.MakeKeyAndVisible ();
    
                return true;
            }
    
        }
    

    然后DialogViewController在ViewDidLoad方法中调整TableView的高度:

       public partial class MyDialogViewController : DialogViewController
        {
            float labelHeight;
    
            public MyDialogViewController (float labelHeight) : base (UITableViewStyle.Grouped, null)
            {
                this.labelHeight = labelHeight;
    
                Root = new RootElement ("MyDialogViewController") {
                    new Section (){
                        new StringElement ("one"),
                        new StringElement ("two"),
                        new StringElement ("three")
                    }
                };
            }
    
            public override void ViewDidLoad ()
            {
                base.ViewDidLoad ();
    
                TableView.Frame = new RectangleF (TableView.Frame.Left, TableView.Frame.Top + labelHeight, TableView.Frame.Width, TableView.Frame.Height - labelHeight);
            }
        }
    

    这是显示模拟器中结果的屏幕截图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-23
      • 2023-03-21
      • 2014-05-16
      • 2019-05-09
      • 2021-05-07
      • 2023-04-05
      • 1970-01-01
      • 2018-12-09
      相关资源
      最近更新 更多