我想創建一個視圖層次結構,當用戶拉動滑塊時,會從屏幕左側「滑出」菜單。該菜單是作爲一個表格與一堆細胞實現的。每個單元格都有一個圖像和一個文本。我正在改變桌子大小的動畫。由於我使用的自動佈局,這裏是我的代碼:當動態調整表格大小時,UITableViewCell內容佈局的奇怪行爲
@interface MyController : UIViewController {
CGFloat size;//0 .. self.frame.size.width * drawerPortion
NSLayoutConstraint *constraint;
UITableViewController *tableViewController;
}
// ...
@end
@implementation MyController
- (void)viewDidLoad {
//...
UIView *view = tableViewController.view;
[self.view addSubview:view];
constraint = [NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:0];
[view addConstraint:constraint];
[self redrawTable];
}
- (void)makeDrawerVisible:(BOOL)visible {
size = visible ? 224 : 0;
[UIView animateWithDuration:0.5
delay:0
options:UIViewAnimationOptionLayoutSubviews
animations:^{[self redrawTable];}
}];
}
- (void)redrawTable {
constraint.constant = size;
[self.view layoutIfNeeded];
}
// ...
@end
的tableViewController
是UITableViewController
一個非常基本的教科書實施。這些單元格創建如下:
UITableCell *cell = [[UITableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.textLabel.text = @"Loren ipsum";
cell.imageView.image = [UIImage imageNamed:@"group"];
最初,我啓動應用程序後,菜單創建爲零寬度,因此被隱藏。當我然後拉動滑塊時,整個事件就會很好地動畫到指定的寬度(224)。下面是我的模擬器截圖,動畫中間的視圖以及最終結果。
怪異的事情開始發生後,我的動畫表回零號。標籤上的文字向左滑動,變得部分被圖像遮擋。下面是它的外觀:
從這一刻起,只要我動畫菜單來回我一直在觀察這個討厭的效果。有沒有人遇到類似的問題?有什麼辦法可以防止標籤在動畫中重新定位?任何有關如何在superview動畫中重新佈置子視圖的良好文檔的指針也將被讚賞。我已經在這個問題上撞了我的頭幾天了,什麼都沒發現。
此替代方法對我無效。我需要菜單滑出背景視圖。否則,菜單右側的功能(邊距)將無法正確顯示。 關於約束和麪具 - 獲得更多信息會很有趣。 UITableViewCell層次結構下沒有任何限制(我將它們記錄下來)。文本標籤上的'autoresizingMask'默認爲零,但將它設置爲'UIViewAutoresizingFlexibleWidth'沒有任何區別。 –