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

标签 ios objective-c uiscrollview autolayout

我有一个如下所示的 View :

View screenshot

红色矩形(引用导出:self.redView)放在 ScrollView (引用导出: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

当我改变矩形的高度然后滚动时,高度变回原来的大小。我该如何防止这种情况?

最佳答案

也许您正在使用自动布局。如果是这样,那就是问题所在。您不能直接在自动布局下更改 View 的框架,因为如果您这样做,当布局出现时,它会立即将其更改回来(就像这里发生的那样)。您必须更改约束条件。

关于iOS - 在运行时调整 UIScrollView 内容的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22969560/

相关文章:

ios - UIScrollView 在新项目中工作完美,但在基于 xib 的项目中没有滚动

ios - UserDefaults UIButton 状态有时加载不正确

ios - 使用UIScrollView,缩小时如何使内容 View 保持在中心?

ios - scrollView 中的多行标签仅显示在一行中并扩展到其全宽

ios - 如何将就地编辑的内容传回之前的tableview controller(更新model)

ios - 将 Facebook 连接到 Swift 4 中的应用委托(delegate)

ios4 中 webview 的 javascript (iscroll4) 滚动解决方案覆盖/禁用 native 手势识别

ios - iOS中如何限制NSNotification多次调用方法?

ios - 如何定位 "unrecognized selector sent to instance"的错误在哪里?

objective-c - 使用自动布局的 UIScrollView 内的完全响应式 UIView