【问题标题】:Changing DialogViewController TableView height using monotouch使用 monotouch 更改 DialogViewController TableView 高度
【发布时间】:2013-07-29 07:14:34
【问题描述】:

在 DialogViewController LoadView 方法上设置 TableView.Frame 似乎没有任何作用是有原因的吗?

目前为了克服这个问题,我将 TableView.ContentInset 设置为顶部,TableView.TableFooterView 设置为底部边距,但这种方法的问题是滚动条不会在表格边界所在的位置开始/结束,例如它会“下” " 底部的 TabBar 等。

有没有办法在DialogViewController中手动设置TableView框架,如果没有,为什么?

谢谢。

更新:我期望应该可以正常工作的示例代码,例如更改 TableView 框架? 注意:如果我在 ViewDidAppear 中设置 Frame 则它可以工作,但我需要它在 ViewDidLoad 中工作。

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

namespace Test
{
    [Register ("AppDelegate")]
    public partial class AppDelegate : UIApplicationDelegate
    {
        UIWindow window;
        public override bool FinishedLaunching (UIApplication app, NSDictionary options)
        {
            var vc = new MainViewController ();
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            window.RootViewController = vc;
            window.MakeKeyAndVisible ();
            return true;
        }
    }

    [Register]
    public class MainViewController : DialogViewController
    {
        public MainViewController (): base (UITableViewStyle.Plain, new RootElement (""))
        {
            TableView.AutoresizingMask = UIViewAutoresizing.None;

            Section s = new Section ("Section 1");
            Root.Add (s);

            StringElement el = new StringElement ("Element 1");
            s.Add (el);

            el = new StringElement ("Element 2");
            s.Add (el);

            s = new Section ("Section 2");
            Root.Add (s);

            el = new StringElement ("Element 1");
            s.Add (el);

            el = new StringElement ("Element 2");
            s.Add (el);
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            TableView.Frame = new RectangleF (0, 120, TableView.Frame.Width, TableView.Frame.Height - 120);
        }
    }
}

【问题讨论】:

标签: uitableview xamarin.ios frame dialogviewcontroller


【解决方案1】:

您是否尝试将TableViewAutoresizingMask 属性设置为UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight

如果这不起作用,请尝试检查 TableViewFrameContentSizeContentInset 属性。看起来TableView.Frame.Bottom 位于您的TabBar 下方,这就是您无法一直滚动到TableView 末尾的原因。

您如何更改TableView.Frame 属性?例如如果你想改变Frame.X,你必须像这样改变整个Frame

RectangleF frame = TableView.Frame
frame.X = 100;
TableView.Frame = frame;

而不仅仅是更改Frame.X:

TableView.Frame.X = 100;

【讨论】:

  • 谢谢。我正在以正确的方式更改 Frame,我认为您甚至不能直接设置它。有趣的是,您可以将表格设置为暂时变小(在 LoadView 中更改框架时),但它会立即调整为全屏。如果我下拉表格视图进行刷新,那么它将正确调整大小,所以看起来 Monotouch 在 LoadView 和 ViewDidAppear 之间的某处更改了框架?
  • 您应该始终在ViewDidLoad 中设置控件、数组等,因为ViewDidLoad 只被调用一次。使用ViewDidAppearViewWillAppear 刷新/重新加载数据。您是设置TableViewRowHeight 属性还是覆盖TableView 的源的GetHeightForRow?即使将TableViewAutoresizingMask 设置为UIViewAutoresizing.None,是否也会发生大小调整?
  • 谢谢 Norbert,我已经用示例更新了我的原始帖子,我认为应该正确调整 tableview 的大小?
  • 我看到了你的回答,但我完全忘记了这一点。很高兴看到你自己想通了!
【解决方案2】:

好的,我想我找到了问题的答案。

DialogViewController 继承自 UITableViewController,据我了解,您无法更改 UITableViewController 内的 TableView 框架。

为此,DialogViewController 需要从 UIViewController 继承,老实说不知道为什么不这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    相关资源
    最近更新 更多