UITableViewCell附件在iPad上太宽了

我在UITableViewCell的iPad上出现问题。 这个问题是在iOS 7和现在之间的某个时候出现的,但是我不确定它何时开始。 在iPod或iPhone上,我的电池看起来很好(纵向或横向),但在iPad上,显示附件很宽。 你可以在下面的图片中看到一个例子。 表格有一个绿色的边框,contentview有一个棕色的边框。 内容视图比应该小得多。 没有披露配件的表格单元看起来不错。

例

我正在使用Xamarin,我正在代码中完全创build这个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的正常工作。

谢谢。 扎克格林

编辑 – 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); } } } 

这是由于苹果iOS9新的可读内容边缘,旨在使内容在更宽的视图(iPad Pro)中更具可读性。

您可以通过设置closures此function

 tableView.cellLayoutMarginsFollowReadableWidth = false 

在viewDidLoad中

试试这个代码:

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

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