我有一个正在创建的消息屏幕,我快完成了。我使用nib文件和约束构建了大多数视图。我有一个小错误,但是由于要求在涉及约束的动画块中调用[self.view layoutIfNeeded],因此在关闭键盘时可以直观地看到一些单元的布局。这是问题所在:
- (void)keyboardWillHide:(NSNotification *)notification
{
NSNumber *duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey];
[UIView animateWithDuration:duration.doubleValue delay:0 options:curve.integerValue animations:^{
_chatInputViewBottomSpaceConstraint.constant = 0;
// adding this line causes the bug but is required for the animation.
[self.view layoutIfNeeded];
} completion:0];
}
是否可以在视图中直接调用布局(如果需要),因为这也会导致我的集合视图自行布局,这有时会使单元格在屏幕上可见。
我尝试了所有可以想到的方法,但找不到该错误修复的解决方案。我已经尝试调用[cell setNeedLayout]; 在所有可能的位置,什么都没有发生。
这个怎么样?
在您的UITableViewCell中实现一个名为MYTableViewCellLayoutDelegate的自定义协议
@protocol MYTableViewCellLayoutDelegate <NSObject>
@required
- (BOOL)shouldLayoutTableViewCell:(MYTableViewCell *)cell;
@end
为此协议创建一个委托@property(非原子的,弱的)id layoutDelegate;
然后在UITableViewCell上覆盖layoutSubviews:
- (void)layoutSubviews {
if([self.layoutDelegate shouldLayoutTableViewCell:self]) {
[super layoutSubviews];
}
}
现在,在您的UIViewController中,您可以实现shouldLayoutTableViewCell:回调以控制是否对UITableViewCell进行布局。
-(void)shouldLayoutTableViewCell:(UITableViewCell *)cell {
return self.shouldLayoutCells;
}
修改keyboardWillHide方法以禁用单元格布局,调用layoutIfNeeded,并在完成块中恢复单元格布局功能。
- (void)keyboardWillHide:(NSNotification *)notification {
NSNumber *duration = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey];
self.shouldLayoutCells = NO;
[UIView animateWithDuration:duration.doubleValue delay:0 options:curve.integerValue animations:^{
_chatInputViewBottomSpaceConstraint.constant = 0;
[self.view layoutIfNeeded];
} completion:completion:^(BOOL finished) {
self.shouldLayoutCells = NO;
}];
}
由于您没有提供示例代码,因此我无法真正测试此功能,但希望这将使您走上正确的道路。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句