Animation restrictions that cause the appearance of the substrate layout on the screen

I have a messaging screen that I create, and I almost did it. I built most of the views with nib files and restrictions. However, I have one small mistake when I can visually see how some of the cells are laid out when the keyboard is rejected due to the requirement to call [self.view layoutIfNeeded] in the animation block with a constraint. Here is the problem:

- (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]; } 

Is there any way to directly call the layout, if necessary, in the view, as it also makes my collection view go all out, which sometimes visually displays cells on the screen.

I tried everything I could, but I can’t find a solution to fix the errors. I already tried to call [cell setNeedLayout]; anywhere, nothing happens.

+7
ios autolayout nslayoutconstraint uicollectionview uiviewanimation
source share
1 answer

How about this?

UITableViewCell implements user protocol MyTableViewCellLayoutDelegate

 @protocol MYTableViewCellLayoutDelegate <NSObject> @required - (BOOL)shouldLayoutTableViewCell:(MYTableViewCell *)cell; @end 

Create a delegate for this protocol @property (non-atomic, weak) id layoutDelegate;

Then override layoutSubviews on your UITableViewCell:

 - (void)layoutSubviews { if([self.layoutDelegate shouldLayoutTableViewCell:self]) { [super layoutSubviews]; } } 

Now, in your UIViewController, you can implement callLayoutTableViewCell: a callback to control the allocation of a UITableViewCell or not.

 -(void)shouldLayoutTableViewCell:(UITableViewCell *)cell { return self.shouldLayoutCells; } 

Modify the keyboardWillHide method to disable cell layout, call layoutIfNeeded, and restore cell layout ability in the completion block.

 - (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; }]; } 

I cannot verify this because you did not provide sample code, but hopefully this puts you on the right track.

+2
source share

All Articles