【问题标题】:UITableViewCell Accessory is too wide on iPadiPad 上的 UITableViewCell 附件太宽
【发布时间】:2015-10-19 21:30:03
【问题描述】:

我的 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;

    }

我尝试通过覆盖单元格的 LayoutSubviews 方法来更改 ContentView 的大小。但是,ContentView 现在与表格一样宽,但显示附件没有改变,现在位于 ContentView 下方。

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

        //ContentView.Frame = new CGRect(0, 0, Frame.Size.Width, ContentView.Frame.Size.Height);
    }

同样,这在 iPhone 或 iPod 上根本不是问题。我不明白我应该做些什么不同的事情才能让 iPad 正常工作。

谢谢。 扎克·格林

编辑 - 2015 年 10 月 21 日 11:00 目前,我已经做了一个非常巧妙的更改来解决我的问题,但我知道未来的 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);
            }
        }
    }

【问题讨论】:

  • 问题是您正在硬编码帧大小:Frame = new CGRect(0, 0, 320, 42); 并设置固定宽度。 iPhone 的屏幕宽度为 320 像素,而 iPad 的屏幕宽度为 768 像素,所以这会破坏您的 UI
  • 那么,我应该省略那行吗?
  • 在更仔细地查看您的代码之后,您似乎没有在任何地方设置 Frame 或 ContentMode 或 AutoresizingMask 属性......为什么要初始化它们呢?
  • 我在 Draw 方法中设置 Frame 和 AutoresizingMask。我想让我烦恼的是,除了带配件的电池外,一切看起来都很好。
  • contentView 看起来是 600 宽,这意味着它正在使用 Any/Any 大小类,这通常意味着它没有大小类。你是如何创建细胞的?确保使用 dequeueReusableCellWithIdentifer:forIndexPath 而不是没有 indexPath 的版本。前者返回一个具有有效大小类的单元格,而后者则没有。

标签: ios uitableview xamarin


【解决方案1】:

这是由于 Apple 在 iOS9 中的新可读内容边距旨在使内容在更广泛的视图中更具可读性 (iPad Pro)。

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

tableView.cellLayoutMarginsFollowReadableWidth = false

在 viewDidLoad 中

【讨论】:

  • "此布局指南定义了一个易于阅读的区域,无需强迫用户移动头部来跟踪线条" ...如果我每本书都有一角钱我因为轻微的头部运动而被扔掉了..
  • 花了 4 个小时终于弄明白了。如果我希望表格或单元格更小,我可以在 2 秒内完成。为什么这些家伙认为他们可以通过打开默认选项来搞乱自定义设计!
【解决方案2】:

试试这个代码:

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

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

            Frame = new CGRect(0, 0, [UIApplication sharedApplication].keywindow.frame.size.width, 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;

        }

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, [UIApplication sharedApplication].keywindow.frame.size.width - 32, ContentView.Frame.Size.Height);

            foreach (var view in Subviews.OfType<UIButton>())
            {
                view.Frame = new CGRect([UIApplication sharedApplication].keywindow.frame.size.width - 24, view.Frame.Y, 8, view.Frame.Height);
            }
        }
    }

【讨论】:

    【解决方案3】:

    使用与[UIApplication sharedApplication].keyWindow.frame.size.width 相同的宽度,而不是硬编码值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2015-04-29
      相关资源
      最近更新 更多