什么是NSLayoutConstraint" UIView的封装,布局,高度"我应该如何去迫使它重新计算干净 [英] what is NSLayoutConstraint "UIView-Encapsulated-Layout-Height" and how should I go about forcing it to recalculate cleanly

查看:238
本文介绍了什么是NSLayoutConstraint" UIView的封装,布局,高度"我应该如何去迫使它重新计算干净的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的UITableView 的iOS 8下运行,我在故事板使用自动单元格的高度,从制约因素。

I have a UITableView running under iOS 8 and I'm using automatic cell heights from constraints in a storyboard.

我的一个细胞中都包含一个的UITextView ,我需要它基于用户输入的收缩和扩张 - 水龙头缩小/扩大文本

One of my cells contains a single UITextView and I need it to contract and expand based on user input - tap to shrink/expand the text.

我通过响应于用户事件添加运行时约束到文本视图和改变对约束的恒定这样:

I'm doing this by adding a runtime constraint to the text view and changing the constant on the constraint in response to user events:

-(void)collapse:(BOOL)collapse; {

    _collapsed = collapse;

    if(collapse)
            [_collapsedtextHeightConstraint setConstant: kCollapsedHeight]; // 70.0
        else
            [_collapsedtextHeightConstraint setConstant: [self idealCellHeightToShowFullText]];

    [self setNeedsUpdateConstraints];

}

Whenver我这样做,我把它包在的tableView 更新和设置的tableView 的需求 updateConstraint:

[tableView beginUpdates];

[_briefCell collapse:!_showFullBriefText];

[tableView setNeedsUpdateConstraints];
// I have also tried 
// [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
// with exactly the same results.

[tableView endUpdates];

当我这样做,我的手机不扩大(与动画,而这样做),但我得到一个约束的警告:

When I do this, my cell does expand (and animates whilst doing it) but I get a constraints warning:

2014-07-31 13:29:51.792 OneFlatEarth[5505:730175] Unable to simultaneously satisfy constraints.

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 

(

    "<NSLayoutConstraint:0x7f94dced2b60 V:[UITextView:0x7f94d9b2b200'Brief text: Lorem Ipsum i...'(388)]>",

    "<NSLayoutConstraint:0x7f94dced2260 V:[UITextView:0x7f94d9b2b200'Brief text: Lorem Ipsum i...']-(15)-|   (Names: '|':UITableViewCellContentView:0x7f94de5773a0 )>",

    "<NSLayoutConstraint:0x7f94dced2350 V:|-(6)-[UITextView:0x7f94d9b2b200'Brief text: Lorem Ipsum i...']   (Names: '|':UITableViewCellContentView:0x7f94de5773a0 )>",

    "<NSLayoutConstraint:0x7f94dced6480 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7f94de5773a0(91)]>"



 )

Will attempt to recover by breaking constraint 

<NSLayoutConstraint:0x7f94dced2b60 V:[UITextView:0x7f94d9b2b200'Brief text: Lorem Ipsum i...'(388)]>

388是我的身高计算从X code。在的UITextView 是我的其他约束/ IB。

388 is my calculated height the other constraints on the UITextview are mine from Xcode/IB.

最后一个是困扰着我 - 我猜 UIView的封装,布局,高度是单元格的高度计算当它第一次呈现 - (我设置我的的UITextView 高度为> = 70.0然而它似乎右)不,这个派生约束,则否决一个更新的用户cnstraint。

The final one is bothering me - I'm guessing that UIView-Encapsulated-Layout-Height is the calculated height of the cell when it is first rendered - (I set my UITextView height to be >= 70.0) however it doesn't seem right that this derived constraint then overrules an updated user cnstraint.

更糟的是,虽然布局code表示,它试图打破我的身高限制,它没有 - 它继续重新计算单元格高度和借鉴一切,因为我想

Worse, although the layout code says it's trying to break my height constraint, it doesn't - it goes on to recalculate the cell height and everything draws as I would like.

那么,什么是 NSLayoutConstraint UIView的封装,布局,高度(我猜它是计算高度自动调整大小的细胞),我应该如何去迫使它重新计算干净?

So, what is NSLayoutConstraint UIView-Encapsulated-Layout-Height (I'm guessing it is the calculated height for automatic cell sizing) and how should I go about forcing it to recalculate cleanly?

推荐答案

尽量降低你的 _collapsedtextHeightConstraint 至999这样的系统提供的优先级 UIView的封装,布局,高度约束始终以precedence。

Try to lower the priority of your _collapsedtextHeightConstraint to 999. That way the system supplied UIView-Encapsulated-Layout-Height constraint always takes precedence.

这是基于你在 -tableView返回的内容:heightForRowAtIndexPath:。确保返回正确的价值和你自己的约束和生成的一个应该是相同的。为自己的约束条件下优先级,而折叠/展开动画是在飞行时,才需要暂时prevent冲突。

It is based on what you return in -tableView:heightForRowAtIndexPath:. Make sure to return the right value and your own constraint and the generated one should be the same. The lower priority for your own constraint is only needed temporarily to prevent conflicts while collapse/expand animations are in flight.

这篇关于什么是NSLayoutConstraint&QUOT; UIView的封装,布局,高度&QUOT;我应该如何去迫使它重新计算干净的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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