2016-07-16 106 views
0

我设置了我拖动到故事板的视图的约束,以便视图覆盖整个屏幕。 (我从顶部,左侧,底部和右侧固定了0)。当视图出现时,我希望视图缩小。该常数应该从0到20之间动画。UIView的动画约束(作为主视图的子视图)

在应用程序中,它会更改常量,但不会为其设置动画效果。此外,在收缩视图的后面,尽管它应该是白色的,但所有东西都是黑色的(因为在该视图后面是主视图是白色的)。

override func viewDidAppear(animated: Bool) { 

    UIView.animateWithDuration(2.5, animations: { 
     self.leftPin.constant = 20.0 
     self.topPin.constant = 20.0 
     self.rightPin.constant = 20.0 
     self.bottomPin.constant = 20.0 
     // bg is my view I want to shrink 
     self.bg.layoutIfNeeded() 
     }) 
} 

我读到,为了将新约束“应用”到实际布局,您必须运行layoutIfNeeded()。但是上面的那些约束也没有这个功能。为什么?

回答

2

@try this

override func viewDidAppear(animated: Bool) { 

    self.leftPin.constant = 20.0 
    self.topPin.constant = 20.0 
    self.rightPin.constant = 20.0 
    self.bottomPin.constant = 20.0 

    UIView.animateWithDuration(2.5, animations: { 

    self.view.layoutIfNeeded() 
    }) 
}