我正在编写一个iOS 7应用程序,我有一个带有标签的矩形。标签是对齐的中心,并被创建为与视图相同的大小(增加大小时的简单性)。
所期望的效果是当点击时,将视图动画为全尺寸,标签始终以中心为中心。
我目前已尝试:
所有这些都不能产生所需的输出。每次标签似乎只是按其最终大小,甚至在视图开始动画。
这是我的代码:
_viewHeightConstraint.constant = self.view.frame.size.height;
_viewWidthConstraint.constant = self.view.frame.size.width;
_viewTopConstraint.constant = 0;
_viewLeftConstraint.constant = 0;
_labelWidthConstraint.constant = self.view.frame.size.width;
_labelHeightConstraint.constant = self.view.frame.size.height;
[self.view needsUpdateConstraints];
[UIView animateWithDuration:1.5f
animations:^(void) {
[self.myView layoutIfNeeded]; //perform relayout of view containing label before relayout of entire view
[self.view layoutIfNeeded];
}];我不知道我是否提供了一切必要的东西,因为我仍然是新的自动收费。然而,所需的效果是一个视图,动画到全尺寸,而标签在中心保持居中。
发布于 2013-10-28 19:27:30
将这些约束添加到标签中:
(UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin)发布于 2013-10-28 23:00:30
你为什么打电话给needsUpdateConstraints?
所有的变化都发生在那里。只需打电话:
...
_labelWidthConstraint.constant = self.view.frame.size.width;
_labelHeightConstraint.constant = self.view.frame.size.height;
[UIView animateWithDuration:1.5f
animations:^(void) {
[self.view layoutIfNeeded];
}];另外,为什么要在标签上设置高度和宽度约束?您只需添加中心、水平和垂直约束。我认为你不能为标签设置垂直文本对齐。但那不是你最初的问题。
https://stackoverflow.com/questions/19643337
复制相似问题