UITableViewCell附件在iPad上太宽

Zac*_*een 21 uitableview ios xamarin

我的UITableViewCell出现在iPad上有问题.这个问题在iOS 7和现在之间出现了,但我不确定它何时开始.在iPod或iPhone上,我的手机看起来很好(纵向或横向),但在iPad上,显示配件非常宽.您可以在下图中看到一个示例.tablecell有一个绿色边框,contentview有一个棕色边框.内容视图比应该小得多.没有公开附件的台式电池看起来很好.

例

我正在使用Xamarin,我在代码中完全创建了这个UI.这是代码(我已经省略了在单元格内部放置ContentView的代码,因为它不相关:

protected void Draw()
    {
        Accessory = UITableViewCellAccessory.DisclosureIndicator;

        Layer.BorderColor = UIColor.Green.CGColor;
        Layer.BorderWidth = 1f;

        Frame = new CGRect(0, 0, 320, 42);
        ContentMode = UIViewContentMode.ScaleToFill;
        AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;

        ContentView.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
        ContentView.MultipleTouchEnabled = true;
        ContentView.ContentMode = UIViewContentMode.Center;
        ContentView.ClipsToBounds = true;
        ContentView.Layer.BorderColor = UIColor.Brown.CGColor;
        ContentView.Layer.BorderWidth = 1f;

    }
Run Code Online (Sandbox Code Playgroud)

我尝试通过覆盖单元格的LayoutSubviews方法来更改ContentView的大小.但是,ContentView现在和表格一样宽,但公开附件没有改变,现在在ContentView下面.

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

        //ContentView.Frame = new CGRect(0, 0, Frame.Size.Width, ContentView.Frame.Size.Height);
    }
Run Code Online (Sandbox Code Playgroud)

同样,这在iPhone或iPod上根本不是问题.我不明白我应该做些什么来让iPad正常工作.

谢谢.扎克格林

编辑 - 10/21/2015 11:00 目前,我做了一个非常hacky的修改,正在修复这个问题,但我知道未来的iOS更新可能会打破这个,所以我更喜欢iOS更多的批准方式去做这个.

public override void LayoutSubviews()
    {
        base.LayoutSubviews();
        //!! - Hack for ipad layout issue with disclosure indicator
        if (Accessory == UITableViewCellAccessory.DisclosureIndicator && UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad)
        {
            ContentView.Frame = new CGRect(0, 0, Frame.Size.Width - 32, ContentView.Frame.Size.Height);

            foreach (var view in Subviews.OfType<UIButton>())
            {
                view.Frame = new CGRect(Frame.Size.Width - 24, view.Frame.Y, 8, view.Frame.Height);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

小智 41

这是因为Apples在iOS9中具有新的可读内容边距,旨在使内容在更广泛的视图中更具可读性(iPad Pro).

您可以通过设置关闭此功能

tableView.cellLayoutMarginsFollowReadableWidth = false
Run Code Online (Sandbox Code Playgroud)

在viewDidLoad中

  • 花了4个小时终于弄明白了。如果我希望表格或单元格更小,我可以在 2 秒内完成。为什么这些家伙认为他们可以通过打开默认选项来处理自定义设计! (3认同)
  • 简单易行,就是诀窍.谢谢 ! (2认同)