我在同一个问题上看了很多不同的主题,似乎无法理解这里发生了什么。我创建了一个非常基本和简单的项目。我想要发生的是一堆元素在滚动视图内部正确滚动。这些元素可能包括独立滚动(或不滚动)的UITableView。以下是我设置基本项目的方法:
在模拟器中运行此操作将导致视图无法滚动。这是预期的,因为所有元素都有足够的空间。然后,我改变了UIScrollView的大小:
在模拟器中运行此操作会导致较小的视图无法滚动。我假设我不是这样做的正确方法。这是我最近尝试过的代码:
ViewController.h
@interface ViewController : UIViewController <UIScrollViewDelegate>
@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIButton *buttonOne;
@property (strong, nonatomic) IBOutlet UIButton *buttonTwo;
@property (strong, nonatomic) IBOutlet UIButton *buttonThree;
@property (strong, nonatomic) IBOutlet UIButton *buttonFour;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.buttonOne setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.buttonTwo setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.buttonThree setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.buttonFour setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.scrollView setTranslatesAutoresizingMaskIntoConstraints:NO];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
非常感谢任何帮助。
答案 0 :(得分:3)
问题是当您在界面构建器中更改滚动视图的大小时,您还将内容的大小设置为与帧大小相同。
您可以在代码中手动增加滚动视图的内容大小,以便您可以像这样滚动它:
// scrollView is an outlet to your scroll view
// Add 200 points to the bottom of the scroll view
[self.scrollView setContentInset:UIEdgeInsetsMake(0, 0, 200, 0)];
尽管如此,只要您以全尺寸绘制子视图,并且使其成为其他视图的子视图,您可能不需要这样做,并让它自动重新调整大小。