iOS 8 - 在启用AutoLayout的情况下更改UIScrollView框架

时间:2014-09-26 17:13:52

标签: ios objective-c uiscrollview autolayout

我正在为iOS 8更新我的应用,并为我的所有观看添加AutoLayout约束。他们大部分都在工作,但UIScrollView (slideUpScrollView)给了我一些麻烦。其中有一个UIButton (toggleDrawerButton)来切换SV的框架。在IB中,存在限制以使SV保持在底部,高度为50pts。当按下toggleButton时,它应该将SV的原点更改为视图的顶部,模拟抽屉效果。

打开/关闭效果有问题。当我按下toggleDrawerButton时,它有时会产生我想要的效果,就像在iOS 7中一样(不调用viewDidLayoutSubviews)。其他时候SV会有一个奇怪的效果,在我的帧移动后调用viewDidLayoutSubviews。然后VDLS将尝试将SV和toggleDrawerButton重新约束回原始的关闭位置。这会导致在打开抽屉时将视图放回到底部。似乎在toggleDrawerButton按下随机调用VDLS。有时它会被调用,有时它不会被调用。

如何在没有自动布局重新约束的情况下正确地重新定位SV的框架以向顶部移动?另外,为什么VDLS有时只在toggleDrawerButton上调用?行为不一致,这使得找到解决方案变得更加困难。

这是toggleDrawerButton的动作代码:

- (void) toggleAddEditDrawer {
if (self.drawerToggle == 0) { // Open the Drawer
    self.drawerToggle = 1;

    self.itemNameTextField.text = nil;
    self.quantityTextField.text = nil;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    //Rotate the button and make the SV frame slide up

    self.toggleDrawerButton.transform = CGAffineTransformMakeRotation(M_PI/4);
    self.slideUpScrollView.frame = CGRectMake(0, self.tableView.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
    self.slideUpScrollView.contentSize = self.slideUpScrollView.frame.size;


    [UIView commitAnimations];
    [self.itemNameTextField becomeFirstResponder];

    NSLog(@"OPEN Toggle - tableview location is: %@",NSStringFromCGRect(self.tableView.frame));
    NSLog(@"OPEN Toggle - scrollview location is: %@",NSStringFromCGRect(self.slideUpScrollView.frame));
    NSLog(@"OPEN Toggle - toggleButton location is: %@",NSStringFromCGRect(self.toggleDrawerButton.frame));


} else { //Close the Drawer
    self.drawerToggle = 0;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.4];
    [UIView setAnimationDelay:0.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    //Reverse the rotation and  make the SV frame slide down
    self.toggleDrawerButton.transform = CGAffineTransformMakeRotation(0);
    self.slideUpScrollView.frame = CGRectMake(0, self.tableView.frame.origin.y + self.tableView.frame.size.height, self.view.frame.size.width, 50);
    self.slideUpScrollView.contentSize = self.slideUpScrollView.frame.size;

    [UIView commitAnimations];
    [self.view endEditing:YES];

    NSLog(@"CLOSE Toggle - tableview location is: %@",NSStringFromCGRect(self.tableView.frame));
    NSLog(@"CLOSE Toggle - scrollview location is: %@",NSStringFromCGRect(self.slideUpScrollView.frame));
    NSLog(@"CLOSE Toggle - toggleButton location is: %@",NSStringFromCGRect(self.toggleDrawerButton.frame));


}
NSLog(@"Drawer Posistion is: %d",self.drawerToggle);

}

编辑:这是一些控制台日志。

工作框架动画日志:
OPEN Toggle - tableview位置为:{{0,2},{320,452}}
OPEN Toggle - scrollview位置为:{{0,2},{320,504}}
OPEN Toggle - toggleButton位置为:{{128.88730162779191,-6.1126983722080901},{62.225396744416173,62.225396744416173}}
抽屉Posistion是:1

VDLS帧重新约束日志:
OPEN Toggle - tableview位置为:{{0,2},{320,452}}
OPEN Toggle - scrollview位置为:{{0,2},{320,504}}
OPEN Toggle - toggleButton位置为:{{128.88730162779191,-6.1126983722080901},{62.225396744416173,62.225396744416173}}
抽屉Posistion是:1
VDLS - tableview位置为:{{0,2},{320,452}}
VDLS - scrollview位置为:{{0,454},{320,50}}
VDLS - toggleButton位置为:{{128.88730162779191,-6.1126983722080901},{62.225396744416173,62.225396744416173}}

1 个答案:

答案 0 :(得分:2)

通常,在使用AutoLayout时,您无法手动操作视图的帧。您需要为约束更改设置动画,而不是帧更改。

以下是动画约束的信息: https://happyteamlabs.com/blog/ios-how-to-animate-autolayout-constraints/

有关启用AutoLayout时设置框架的更多信息: Can I use setFrame and autolayout on the same view?