Mel*_*emi 122 uiview ios autolayout nslayoutconstraint
我以前从未使用过autolayout约束.我有一个小的新应用程序我正在研究并注意到NIB的观点默认为自动布局.所以,我想我会抓住机会与它一起努力,并试图找出Apple的目标.
第一个挑战:
我需要调整MKMapView的大小,我想将它设置为新位置.如果我按照以前的方式这样做:
[UIView animateWithDuration:1.2f
animations:^{
CGRect theFrame = worldView.frame;
CGRect newFrame = CGRectMake(theFrame.origin.x, theFrame.origin.y, theFrame.size.width, theFrame.size.height - 170);
worldView.frame = newFrame;
}];
Run Code Online (Sandbox Code Playgroud)
...然后,只要兄弟视图得到更新,MKMapView就会"快速"回到原来的高度(在我的情况下,正在更新UISegmentedControl的标题[myUISegmentedControl setTitle:newTitle forSegmentAtIndex:0]).
所以,我觉得我想要做的是从等于父视图的HIGHT到可相对于UISegmentedControl的顶部,它改变的MKMapView的限制被覆盖:V:[MKMapView]-(16)-[UISegmentedControl]
我想要的是缩短MKMapView高度,以便显示地图视图下方的一些控件.为此,我认为我需要将约束从固定的全尺寸视图更改为底部被约束到UISegmentedControl顶部的约束...并且我希望它在视图缩小到新大小时进行动画处理.
怎么会这样呢?
编辑 - 虽然视图底部立即向上移动170,但此动画不是动画:
[UIView animateWithDuration:1.2f
animations:^{
self.nibMapViewConstraint.constant = -170;
}];
Run Code Online (Sandbox Code Playgroud)
并且nibMapViewConstraint在IB中连接到底部垂直空间约束.
Ed *_*nus 177
更新约束后:
[UIView animateWithDuration:0.5 animations:^{[self.view layoutIfNeeded];}];
Run Code Online (Sandbox Code Playgroud)
替换self.view为对包含视图的引用.
Dev*_*evC 86
这对我有用(iOS7和iOS8 +).单击要调整的自动布局约束(在界面构建器中,例如顶部约束).接下来让它成为IBOutlet;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
Run Code Online (Sandbox Code Playgroud)
动画向上;
self.topConstraint.constant = -100;
[self.viewToAnimate setNeedsUpdateConstraints];
[UIView animateWithDuration:1.5 animations:^{
[self.viewToAnimate layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)
动画回到原始位置
self.topConstraint.constant = 0;
[self.viewToAnimate setNeedsUpdateConstraints];
[UIView animateWithDuration:1.5 animations:^{
[self.viewToAnimate layoutIfNeeded];
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77286 次 |
| 最近记录: |