2014-09-22 22 views
3

由於更新到iOS8,很多動畫已停止工作。看起來視圖的y位置不能改變。在iOS7中沒有問題。但對於iOS8,它不會超過原來的y的位置。看起來,視圖的框架確實正在更新,但框架未被重繪。這裏是我的代碼UIView transitionWithView&設置框架不再有效iOS8

[UIView transitionWithView:self.view duration:0.3 options:UIViewAnimationOptionCurveEaseInOut animations:^{ 

self.table.frame = CGRectMake(0, (self.view.bounds.size.height - self.button.frame.size.height) - (252), self.view.bounds.size.width, self.table.frame.size.height); 


} completion:nil]; 

我也試過這段代碼在動畫塊之外;

self.table.frame = CGRectMake(0, (self.view.bounds.size.height - self.button.frame.size.height) - (252), self.view.bounds.size.width, self.table.frame.size.height); 

回答

4

更新的解決方法是調整自動佈局限制,而不是frames原點。點擊您想要調整的自動佈局約束(在界面構建器中,例如頂部約束)。接下來做一個IBOutlet;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topConstraint; 

然後進行動畫處理;

self.topConstraint.constant = -100; 
[self.viewToAnimate setNeedsUpdateConstraints]; 
    [UIView animateWithDuration:.3 animations:^{ 
     [self.viewToAnimate layoutIfNeeded]; 
    }]; 

上述代碼將移動視圖/或向上移動約束。將其撤回原來的簡單呼叫;

self.topConstraint.constant = 0; 
[self.viewToAnimate setNeedsUpdateConstraints]; 
    [UIView animateWithDuration:.3 animations:^{ 
     [self.viewToAnimate layoutIfNeeded]; 
    }]; 

第二種解決 我找到了解決辦法。看來,如果self.tableIBOutlet那麼動畫將無法正常工作。我猜這是新動態故事板的結果。在代碼中而不是在故事板中創建所有元素將導致成功的動畫。這就是我必須要做的所有事情,在代碼中創建UITableView

+0

如果有人也堅持這一點,那麼不要使用[self.viewToAnimate layoutSubviews];內部動畫塊,因爲它不工作。使用[self.viewToAnimate layoutIfNeeded];代替。 – Borzh 2015-04-20 17:24:10

+0

@Borzh多數民衆贊成在什麼原始答案說? – DevC 2015-04-20 18:46:39

+0

是的,我只是想強調layoutIfNeeded方法是必要的,而不是layoutSubviews。問題是layoutSubviews在iOS 7.1上工作,而在iOS 8+上沒有 – Borzh 2015-04-20 18:48:52