【问题标题】:Autolayout constraint is not working after animation [duplicate]动画后自动布局约束不起作用[重复]
【发布时间】:2015-06-26 12:18:26
【问题描述】:

我对 AutoLayout 有点陌生。我已经知道有关此 autoLayout 的大量问题和教程,但我还没有找到我的解决方案。所以提前感谢您的帮助。

我的要求是什么?

我必须制作 UIView,它会在从屏幕底部按下带有动画的按钮后出现在屏幕上。(就像键盘一样)。我已经使用 autoLayout 在 xib 文件中制作了这个 UIView。到目前为止,我已经完成了这个。

在 ViewDidLoad 中:

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

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

在 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");
   }];
}

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

问候 埃蒙。

【问题讨论】:

标签: ios objective-c iphone autolayout nslayoutconstraint


【解决方案1】:

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

声明一个属性:

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

在实现中:

heightConstraint = self.view.frame.size.height;

这篇文章也可能会有所帮助: AutoLayout, Constraints and Animation

【讨论】:

    【解决方案2】:

    如果您使用的是NSAutoLayout,则不应重新构建。

    1. 您必须在屏幕底部安装一个带有滚动条和视图父级的约束。请记住在实例化自定义视图时将 translatesAutoresizingMaskIntoConstraints 设置为 NO。

    1. 创建此约束的 Outlet。
    2. 当你需要改变动画时

      _ctScrollBottom.constant = self.view.frame.size.height-containerView.frame.size.height;
      
      [UIView animateWithDuration:animationDuration
                       animations:^(){
                           [self.view  layoutIfNeeded];
                       }];
      

    我计算了你的“y”位置,但我没有测试过;)

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

        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];`
          

        将此约束作为属性:

        @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/

        希望对你有帮助

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-04
          • 2019-06-05
          • 1970-01-01
          • 2018-04-27
          • 1970-01-01
          相关资源
          最近更新 更多