gpt4 book ai didi

view - 如何将对话框 View Controller 作为 subview 添加到 UIView 或反之亦然?

转载 作者:行者123 更新时间:2023-12-04 22:49:34 24 4
gpt4 key购买 nike

我在网上环顾了一段时间,寻找有关此主题的任何资源,但没有想出任何解决方案来解决我的困境。

我有一个对话框 View Controller ,它的根只是显示一个字符串列表,类似于 iphone 音乐歌曲可 ScrollView 的布局方式。我需要的是位于屏幕顶部的 subview 和它下方的可滚动 DVC。我需要顶部 View 始终在 View 中,而用户可以滚动根元素,因为顶部 View 将保存统计信息。

我曾尝试添加一个 subview ,但它只是与它下面的 dvc 重叠,而且我无法找到一种将 dvc 作为 subview 添加到 UIView 的方法。

任何帮助将非常感激。

最佳答案

实现这一点需要一个单一的根 View Controller ,它承载两个 subview Controller 。一个 subview 包含窗口顶部的统计信息。底部 subview 包含一个导航 Controller ,用于保存对话框 View 。

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;
}
}
}

关于view - 如何将对话框 View Controller 作为 subview 添加到 UIView 或反之亦然?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10644260/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com