Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用自动布局时,iOS 8上的旋转单元格高度丢失_Ios_Rotation_Ios8_Autolayout_Nslayoutconstraint - Fatal编程技术网

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

在使用自动布局时,iOS 8上的旋转单元格高度丢失,ios,rotation,ios8,autolayout,nslayoutconstraint,Ios,Rotation,Ios8,Autolayout,Nslayoutconstraint,通常iOS 8应该能够计算单元本身的高度。到目前为止,这是可行的,但如果我旋转设备,电池就会回到44点的标准高度 它应该是这样的: 这是旋转后的情况: 旋转后,它将保持在该布局中。它不再计算实际高度。我不知道为什么。我已经添加了完整的代码。对于约束,请查看updateConstraints。代码是用C#编写的,但您应该能够阅读它。当然,你可以在Objective-C中发布你的解决方案 VCviewDidLoad: public override void ViewDidLoad () {

通常iOS 8应该能够计算单元本身的高度。到目前为止,这是可行的,但如果我旋转设备,电池就会回到44点的标准高度

它应该是这样的:

这是旋转后的情况:

旋转后,它将保持在该布局中。它不再计算实际高度。我不知道为什么。我已经添加了完整的代码。对于约束,请查看
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是另一个故事。

viewwilltransitionosize
函数中,设置
self.tableView.estimatedRowHeight=您估计的行高
,它似乎能够解决问题,也许是苹果的一个bug,将来可以改进。

viewwilltransitionosizeosize
函数中,set
self.tableView.estimatedroweight=您估计的行高
,它似乎能够解决这个问题,也许这是苹果公司的一个缺陷,将来可以改进。

在viewwilltransitionosize函数中,set
self.tableView.estimatedroweight=您估计的行高
@gabbler:似乎可以做到这一点。但我为什么需要这个?实现
EstimatedHeightForrowatingIndexPath
会做同样的事情吗?好的,我试过了,但没有做同样的事情。另外,在
中设置预计行高将导致RotateToInterfaceOrientation:duration无效。奇怪…如果您在ViewDid中添加
TableView.RowHeight=UITableViewAutomaticDimension
,加载并删除
SetNeedsUpdateConstraints
UpdatesUpdateConstraints
函数,会怎么样?如果没有
SetNeedsUpdateConstraints
UpdatesUpdatesUpdatesIfNeeds
函数,表视图将完全为空,并且也有它是(单元格的)默认大小<代码>this.TableView.RowHeight=UITableView.AutomaticDimension单独使用没有帮助。在这些测试用例中,我根本没有使用
estimateDroweight
。我应该这样做吗?在ViewWillTransitionOnSize函数中,设置
self.tableView.estimatedRowHeight=您估计的行高
@gabbler:似乎可以做到这一点。但我为什么需要这个?实现
EstimatedHeightForrowatingIndexPath
会做同样的事情吗?好的,我试过了,但没有做同样的事情。另外,在
中设置预计行高将导致RotateToInterfaceOrientation:duration无效。奇怪…如果您在ViewDid中添加
TableView.RowHeight=UITableViewAutomaticDimension
,加载并删除
SetNeedsUpdateConstraints
UpdatesUpdateConstraints
函数,会怎么样?如果没有
SetNeedsUpdateConstraints
UpdatesUpdatesUpdatesIfNeeds
函数,表视图将完全为空,并且也有它是(单元格的)默认大小<代码>this.TableView.RowHeight=UITableView.AutomaticDimension单独使用没有帮助。在这些测试用例中,我根本没有使用
estimateDroweight
。我应该吗?现在我从苹果那里得到了反馈:需要设置一个估计的行高,才能使自调整大小的单元格正常工作。很好,你是如何得到反馈的?我填写了一份错误报告,苹果开发者关系发给我这个答案。对我来说很有效。和+1用于填充bugreport。现在我从苹果得到了反馈:需要设置一个估计的行高才能使自调整大小的单元格工作。很好,你是如何得到反馈的?我填充了bugreport,苹果开发者关系给我这个答案。对我来说很有用。和+1用于填写错误报告。