iOS 8:UITableViewCell.contentView不会在其中调整子视图的大小 [英] iOS 8: UITableViewCell.contentView doesn't resize subviews in it

查看:331
本文介绍了iOS 8:UITableViewCell.contentView不会在其中调整子视图的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在UITableViewCell.contentView中放置了CustomView,将自定义视图的autoresize mask设置为W+H.

I put an CustomView in UITableViewCell.contentView, setting the autoresize mask of the custom view as W+H.

但是运行时,CustomView的高度大于ContentViewContentView的高度为60pt(与UITableViewDelegate的设置相同),但是内部的CustomView contentView的高度为76pt.

But when running, the CustomView gets a larger height than the ContentView, the ContentView is 60pt in height(which is the same as setting in UITableViewDelegate), but the CustomView inside contentView is 76pt in height.

通过Xcode6's view debugging,我在自定义视图上看到了一些奇怪的约束,它们是:

By Xcode6's view debugging, I see some strange constraints on my custom view, they are:

self.height = superview.height + 16

self.midY = superview.midY + 8

这些约束来自何处,以及如何对其进行修改?我从来没有设置任何值为8或16的值.

Where are these constraints come from and how to modify them? I've never set anything with a value 8 or 16.

更新:

我已经创建了一个测试项目,它只是情节提要中的一个表视图,并从nib文件中加载了CustomView,并且该测试项目在运行时复制了CustomView的问题,该CustomViewTableViewCell.contentView的高度大于TableViewCell.contentView.

I've made a test project, which is simply a tableview in storyboard with a CustomView loaded from a nib file, and this test project replicated the problem, when running, the CustomView which is a subview of TableViewCell.contentView becomes larger in height than the TableViewCell.contentView.

测试项目在这里: https://drive.google.com/file/d/0B5y_NrRbhGlSb1dlbVZNb19vNjQ ?usp =分享

推荐答案

最后,我了解到,自动布局只能在同一xib或情节提要中形成关系,我的CustomView位于单独的xib中,并在运行时加载,因此超级视图和CustomView之间没有任何自动布局约束.

At last I understand, auto layout can only form relations in the same xib or storyboard, my CustomView is in a separated xib and is loaded at runtime, so the super view and CustomView don't have any auto layout constraints between them.

如果我将translatesAutoresizingMaskIntoConstraints设置为YES,则会出现问题,我仍然不知道为什么使用W + H作为自动调整大小蒙版会使CustomView高于其超级视图(cell.contentView),但是我发现一个解决方法:

If I set translatesAutoresizingMaskIntoConstraints to YES, then there goes the problem, I still don't know why using W+H as auto resizing mask makes CustomView taller than its super view(cell.contentView), but I found a way around:

我手动在superview和CustomView之间添加约束,然后关闭customView. translatesAutoresizingMaskIntoConstraints,代码如下:

I manually add constraints between superview and CustomView, and turn off customView. translatesAutoresizingMaskIntoConstraints, there the code goes:

    customView.translatesAutoresizingMaskIntoConstraints = NO;

    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:0];
    NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:0];
    NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:customView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeRight multiplier:1 constant:0];

    [cell.contentView addConstraints:@[top, bottom, left, right]];

这篇关于iOS 8:UITableViewCell.contentView不会在其中调整子视图的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆