Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-在运行时调整UIScrollView内容的大小_Ios_Objective C_Uiscrollview_Autolayout - Fatal编程技术网

iOS-在运行时调整UIScrollView内容的大小

iOS-在运行时调整UIScrollView内容的大小,ios,objective-c,uiscrollview,autolayout,Ios,Objective C,Uiscrollview,Autolayout,我的视图如下所示: 红色矩形(引用outlet:self.redView)放在scrollview(引用outlet:self.scrollview)的内部。当按下“较大”或“较小”按钮时,红色矩形将展开/收缩其高度 我的代码在这里: #import "s1cViewController.h" @interface s1cViewController () @end @implementation s1cViewController - (void)viewDidLoad {

我的视图如下所示:

红色矩形(引用outlet:self.redView)放在scrollview(引用outlet:self.scrollview)的内部。当按下“较大”或“较小”按钮时,红色矩形将展开/收缩其高度

我的代码在这里:

#import "s1cViewController.h"

@interface s1cViewController ()

@end

@implementation s1cViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidLayoutSubviews {
    [self.scrollView setScrollEnabled:YES];
    [self.scrollView setContentSize:CGSizeMake(self.scrollView.frame.size.width, 600)];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnBiggerTouched:(id)sender {
    [self resizeViewHeight:20];
}

- (IBAction)btnSmallerTouched:(id)sender {
    [self resizeViewHeight:-20];
}

- (void)resizeViewHeight:(int)heightDelta {
    [UIView animateWithDuration:0.2f animations:^{
        // Resize scrollview height
        /*self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.scrollView.contentSize.height + deltaInputWrapperHeight);*/

        // Resize categoryInputView height
        CGRect rect = self.redView.frame;
        rect.size.height += heightDelta;
        self.redView.frame = rect;
    }];
}

@end

当我更改矩形的高度,然后滚动时,高度会更改回其原始大小。如何防止这种情况发生?

也许您正在使用自动布局。如果是这样,那就是问题所在。您不能直接在“自动布局”下更改视图的框架,因为如果您这样做,当布局出现时,它只会将其重新更改(这里似乎发生了这种情况)。您必须更改约束。

有没有办法设置动画?当然可以。最简单的方法就是对约束中的更改设置动画。请参见我对该主题的完整讨论: