我有一个如下所示的 View :
红色矩形(引用导出: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/