'Must translate autoresizing mask into constraints to have _setHostsLayoutEngine:YES.

Xcode报错如题

重现错误情景:

在使用自动布局完成tableViewCell的自适应时出现该问题。

如何使用AutoLayout实现tableViewCell的自适应:

1.使用AutoLayout对cell的子视图设置在contentView中自上而下完整的垂直约束。

添加约束时的要点:a.一定要保持完整的自上至下的约束;b.为放置imageView的UIView类型的控件添加约束时将高度设置为0,它会自动根据子视图的内容和大小进行高度自适应。当然也要给其中使用代码添加的子视图使用代码进行约束布局。

2.给tableViewCell设置预估高度。

3.对xib cell文件进行注册复用即可自适应高度

编程中永远相信你不是第一个遇到该问题的人。

cocoaChina上也有人提问了该问题,我是链接,但请不要点我,但是回答很。。。

http://www.cocoachina.com/bbs/read.php?tid=253807

看到这种答案挺尴尬的,唯一的好处就是知道有难兄难弟在:

难兄难弟 


当然该问题之下也有正常的回答,但是也都没有指出问题所在。

回答的就像:电脑死机怎么办?重装系统呗。我电脑word打不开了怎么办?重装系统呗。。。

Stack overflow上的回答更为全面合理链接如下:点击我可直接访问

http://stackoverflow.com/questions/24217195/why-do-i-get-must-translate-autoresizing-mask-into-constraints-to-have-sethost    

该问题的回答非常详尽,可是没一个是我的情况,所以我一遍遍的改约束改到脑袋快要爆炸。可以访问网站的建议直接去访问该问题,回答很详细。(当然也有直接丢锅给Xcode的**)

不想看的我就简单概括一下该问题:当UIView和UITableViewCell两个类型的实例发生交叉感染时就出现该问题。

会出现该问题的情况包括:

1.使用UIView类型的实例当作UITableViewCell使用

2.将UITableViewCell添加到了UIView类型的实例上

3.设置属性里将UIView和UITableViewCell两者类型弄混

解决:细心认真的设置好属性与对应视图的类型

问题:

I have the following code in a swift UITableViewController, but i get

a "Must translate autoresizing mask into constraints to have

_setHostsLayoutEngine:YES exception at the 2nd line. I didnt change any

settings in the interface builder (so Autolayout and size classes are

both checked).

override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell? {

let cell:TodoItemCell = tableView.dequeueReusableCellWithIdentifier("TodoItemCell", forIndexPath: indexPath) as TodoItemCell

let row = indexPath.row

cell.titleLabel.text  = self.todoItems![row].title

self.callback!(row)

// Configure the cell...

return cell

}

What am i doing wrong?

回答:

1.Not sure how much this helps, but I see the same error if I try to

place a cell directly in a UIView rather than in a table. This worked

fine with Xcode 5 so I suspect it is a bug with XCode6/ios8. In the mean

time, debug into that method and look at your tableView. Make sure it

actually has an instance of the cell you are trying to retrieve.

Update:Filed a bug report with Apple and I can confirm that this is an ios8 issue. Unfortunately it is still there as of beta-5, hopefully they will have it fixed before the final release.

2.This isNOTa bug, you simply cannot use aUIViewas cell for table view, you must make sure the top level view in the nib file is aUITableViewCell.

3.In my case, I was loading a nib file with a UITableViewCell contained in

a UIView. After disabling autolayout in the nib file, I haven't got the

exception again.

4.I also ran into this issue and it was due to a UITableViewCell subclass being used in the view hierarchy outside of a UITableView as noted in the other answers.  In this case I had limited time, so I couldn't move the functionality present in that subclass into a UIView subclass.  The workaround I came up with was to just create a UIView instance in my view hierarchy where the cell was supposed to be and transplant the view hierarchy of the cell instance into it, and remove the cell itself from the displayed view hierarchy.  So long as I kept the views I moved wired to the various properties of the cell instance, everything keeps working fine.

It's a bit hacky, but it will keep things running until the time to refactor is available.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

  • 每一个故事都会有一个幸福的结局,如果你还不曾感到幸福,请等下去吧,因为你的故事,未完待续。
    海星love_smile阅读 192评论 0 1
  • 520?情侣约会的一天,秀恩爱的人占据了空间,不久之前我也以为那个人会陪我很久,可是并没有。也许是自己太过于现实,...
    心之璃阅读 184评论 0 0