gpt4 book ai didi

ios - 使用自动布局时,iOS 8 上的旋转单元格高度会丢失

转载 作者:行者123 更新时间:2023-11-28 19:00:02 27 4
gpt4 key购买 nike

通常 iOS 8 应该能够计算单元格本身的高度。到目前为止这是有效的,但如果我旋转设备,单元格将恢复到其标准高度 44 点。

它应该是这样的: This is working

这是旋转后的样子: After rotation

一旦旋转,它就停留在那个布局中。它不再计算真实高度。我不知道为什么。我已经添加了完整的代码。对于约束,请查看 updateConstraints。代码在 C# 中,但您应该能够阅读它。您当然可以在 Objective-C 中发布您的解决方案。

VC viewDidLoad:

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

this.TableView.Source = new TestSource (this);
this.TableView.RegisterClassForCellReuse (typeof(CustomCell), TestSource.cellIdentifier);
//this.TableView.RowHeight = 83;
}

我的数据来源:

public class TestSource : UITableViewSource
{
public static readonly NSString cellIdentifier = new NSString("CustomCell");
private TestTableVC controller;

public TestSource (TestTableVC controller)
{
this.controller = controller;
}

public override int RowsInSection (UITableView tableview, int section)
{
return 10;
}

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
CustomCell cell = tableView.DequeueReusableCell (cellIdentifier) as CustomCell;

cell.UpdateCell ("Max Mustermann", "m", new DateTime (2014, 12, 10), "1234567890");

cell.SetNeedsUpdateConstraints ();
cell.UpdateConstraintsIfNeeded ();

return cell;
}
}

我的CustomCell:

public class CustomCell : UITableViewCell
{
public static readonly NSString cellIdentifier = new NSString("CustomCell");

private UILabel fullNameLabel, genderLabel, birthdateLabel, iNumberLabel;
private bool didSetupConstraints = false;

public CustomCell ()
{
this.CreateView ();
}

public CustomCell (IntPtr handle) : base(handle)
{

this.CreateView ();
}

public void UpdateCell (string fullName, string gender, DateTime? birthdate, string iNumber)
{
fullNameLabel.Text = fullName;
genderLabel.Text = "Gender" + ": " + gender;
birthdateLabel.Text = "Born on" + ": " + dateOfBirth.ToString ("dd.MM.yyyy");
iNumberLabel.Text = iNumber;
}

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

this.ContentView.SetNeedsLayout();
this.ContentView.LayoutIfNeeded();
}

public override void UpdateConstraints()
{
if (!didSetupConstraints) {
NSMutableDictionary viewsDictionary = new NSMutableDictionary ();
viewsDictionary ["fullNameLabel"] = fullNameLabel;
viewsDictionary ["genderLabel"] = genderLabel;
viewsDictionary ["birthdateLabel"] = birthdateLabel;
viewsDictionary ["iNumberLabel"] = iNumberLabel;

fullNameLabel.TranslatesAutoresizingMaskIntoConstraints = false;
genderLabel.TranslatesAutoresizingMaskIntoConstraints = false;
birthdateLabel.TranslatesAutoresizingMaskIntoConstraints = false;
iNumberLabel.TranslatesAutoresizingMaskIntoConstraints = false;


// Sizing of content view is differently in iOS 7 (autoresizing mask) and iOS 8 (layoutSubViews).
// Don't know why this occurs but it should only concern nibs and if you are using iOS 8 SDK and compile for iOS 7.
// http://stackoverflow.com/questions/24750158/autoresizing-issue-of-uicollectionviewcell-contentviews-frame-in-storyboard-pro
// http://stackoverflow.com/questions/19132908/auto-layout-constraints-issue-on-ios7-in-uitableviewcell
// bug?
if (!UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
// only one of these statements is needed
this.ContentView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;
this.ContentView.Bounds = new RectangleF (0, 0, 99999, 99999);
}

this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-[fullNameLabel]", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-[genderLabel]|", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:|-[birthdateLabel]", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("V:|-(8)-[fullNameLabel]-(5)-[genderLabel]-(5)-[birthdateLabel]-(8)-|", (NSLayoutFormatOptions)0, null, viewsDictionary));
this.ContentView.AddConstraint (NSLayoutConstraint.Create (iNumberLabel, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this.ContentView, NSLayoutAttribute.CenterY, 1, 0));
this.ContentView.AddConstraints (NSLayoutConstraint.FromVisualFormat ("H:[iNumberLabel]-|", (NSLayoutFormatOptions)0, null, viewsDictionary));

didSetupConstraints = true;
}

base.UpdateConstraints ();
}

private void CreateView()
{
fullNameLabel = new UILabel () {
Font = UIFont.BoldSystemFontOfSize(17),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Left
};
genderLabel = new UILabel () {
Font = UIFont.SystemFontOfSize(15),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Left
};
birthdateLabel = new UILabel () {
Font = UIFont.SystemFontOfSize(15),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Left
};
iNumberLabel = new UILabel () {
Font = UIFont.SystemFontOfSize(15),
TextColor = UIColor.Black,
BackgroundColor = UIColor.Clear,
LineBreakMode = UILineBreakMode.TailTruncation,
Lines = 0,
TextAlignment = UITextAlignment.Right
};

ContentView.AddSubviews (fullNameLabel, genderLabel, birthdateLabel, iNumberLabel);

}

}

我的约束有问题还是这是另一个 iOS 8 错误?顺便说一句:我将 Xcode 6.1 与 iOS 8.1 SDK 一起使用,并希望支持 iOS 7 作为 iOS 8 设备。 iOS 7 是另一回事。

最佳答案

viewWillTransitionToSize函数中,设置self.tableView.estimatedRowHeight = your estimated row height,似乎可以解决问题,也许是来自的bug future 可以改进的苹果。

关于ios - 使用自动布局时,iOS 8 上的旋转单元格高度会丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26881764/

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