7

我有一個消息屏幕我正在創建,我差不多完成了它。我用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];在每一個可能的地方,都沒有任何反應

+0

當細胞自行排列時,你會看到什麼?你的UITableViewCell中有layoutSubviews覆蓋嗎? – dfmuir 2014-10-05 03:03:35

+0

我的單元格看起來像iMessage(聊天泡泡)的精確副本,我可以直觀地看到鍵盤解散後回到屏幕上的單元格中生長的聊天泡泡。它不會發生在正常滾動時,只有在鍵盤關閉後視圖放下時纔會發生....不,我沒有在單元格的佈局子視圖中使用任何代碼。 – DBoyer 2014-10-05 03:08:09

+0

我認爲它的實際動畫增長的泡沫......很奇怪。 – DBoyer 2014-10-05 03:14:10

回答

2

這個怎麼樣?

在你的UITableViewCell實現所謂MYTableViewCellLayoutDelegate

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

@end 

創建此協議 @屬性(非原子,弱)ID layoutDelegate委託定製協議;

然後重寫layoutSubviews在你的UITableViewCell:現在

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

我真的不能測試這個,因爲你沒有提供的示例代碼,但希望這將讓你在正確的道路上。

+0

哇好主意!我現在要測試這個。 – DBoyer 2014-10-05 03:54:38

+0

很酷。讓我知道它是否有效。 – dfmuir 2014-10-05 03:59:39

+0

可悲的是沒有運氣。 :(一切都發生完全一樣的方式 – DBoyer 2014-10-05 04:02:57