动画后,Autolayout约束无效

时间:2015-06-26 12:18:26

标签: ios objective-c iphone autolayout nslayoutconstraint

我在AutoLayout上有点新鲜。我已经知道关于这个autoLayout的大量问题和教程,但我还没有找到我的解决方案。所以提前感谢您的帮助。

我的要求是什么?

我必须制作UIView,它会在屏幕底部按下一个按钮后进入屏幕。动画。(像键盘一样)。我使用autoLayout在xib文件中制作了这个UIView。所以我做的很像此

在ViewDidLoad中:

//Loading custom UIView 
containerView = [[SharingPickerView alloc] init];
[containerView loadingMenus:pickerData];
[self.view addSubview:containerView];

在这个视图中,它是contatains(滚动视图,然后是页面控制器,然后是取消按钮)

在ViewWillLayoutSubviews中:

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];

//Changing the frame of view to bottom side of the screen so that we can animate it later from bottom to top later.

containerView.frame = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, containerView.frame.size.height);
[containerView setBackgroundColor:[[UIColor colorWithRed:231.0/255.0f green:231.0/255.0f blue:231.0/255.0f alpha:1.0f] colorWithAlphaComponent:0.3]];
}

现在正在按动画。

-(void)sharePickerView{
   [UIView animateWithDuration:1 animations:^(){
   containerView.frame = CGRectMake(0,self.view.frame.size.height-containerView.frame.size.height, self.view.frame.size.width, containerView.frame.size.width);
   } completion:^(BOOL isFinished){
  NSLog(@"Animation Finish");
   }];
}

在这里,我正在重新构建View以显示动画,但是在iPhone 6模拟器中没有显示某些底部部分(该视图的取消按钮),但它显示完美的iPhone 5s设备。

此致 卫门。

4 个答案:

答案 0 :(得分:1)

尝试使用大小限制作为出口。然后,您可以轻松更改这些约束的值。使用自动布局时,不建议修改帧。

声明一个属性:

onedit ()

并在实施中:

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

这篇文章也许有帮助: AutoLayout, Constraints and Animation

答案 1 :(得分:1)

如果您使用的是NSAutoLayout,则不应重新设定。

  1. 您必须使用滚动和视图父级在屏幕底部安装约束。请记住,在实例化自定义视图时,将translatesAutoresizingMaskIntoConstraints设置为NO。
  2. enter image description here

    1. 创建此约束的插座。
    2. 当您需要更改动画

      _ctScrollBottom.constant = self.view.frame.size.height-containerView.frame.size.height;
      
      [UIView animateWithDuration:animationDuration
                       animations:^(){
                           [self.view  layoutIfNeeded];
                       }];
      
    3. 我进行了“y”位置计算,但我没有对其进行测试;)

      希望这有帮助!

答案 2 :(得分:0)

我建议你以编程方式添加约束

  1. 首先,将translatesAutoresizingMaskIntoConstraints设置为NO

    containerView = [[SharingPickerView alloc] init];
    containerView.translatesAutoresizingMaskIntoConstraints = NO;
    [containerView loadingMenus:pickerData];
    [self.view addSubview:containerView];
    
  2. 第二,添加高度约束

    // for example, half height as view.
    
    self.heightConstraint = [NSLayoutConstraint
          constraintWithItem:containerView
                   attribute:NSLayoutAttributeHeight
                   relatedBy:NSLayoutRelationEqual
                      toItem:self.view
                   attribute:NSLayoutAttributeHeight
                  multiplier:0.5
                    constant:0.0]
    [self.view addConstraint:self.heightConstraint];`
    
  3. 将此约束保持为属性:

    @property (nonatomic, strong) NSLayoutConstraint *heightConstraint;
    

    动画约束更改为

    [UIView animateWithDuration:0.5
                     animations:^{
                         self.heightConstraint.constant += Value_to_change;
                         [self.view layoutIfNeeded];
                     }];
    

    另外,不要忘记添加尾随和前导约束+顶部约束

    这是一个很好的教程 http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/

    希望这有帮助

答案 3 :(得分:0)

这个答案https://stackoverflow.com/a/26040569/2654425可能非常有帮助,因为它处理类似的情况。