1

第二次更新

在iOS6
、XCode 4.5 中,UIScrollView 不滚动,尽管设置了 contentSize

答案已经给出!关闭 Storyboard 上的 Auto Layout(整个事情)将修复 ScrollView 并允许事情正确滚动。传播大众!

更新添加的代码。严重无法弄清楚为什么这在 iPad 上不起作用。

.h 文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

.m 文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
self.scrollView.contentSize = CGSize(768, 1600);
}

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

@end

再次澄清我正在使用 Storyboards,我首先拖入一个 ScrollView,将大小设置为标准 iPad 屏幕(768x1004,因为我在顶部有一个导航栏),然后在上面添加我的图像并设置它的大小(768x1600)。然后我将这两个都连接到头文件(如上所示),但我仍然无法滚动图像。我的视图模式设置为“Aspect Fit”,这可能是问题吗?我尝试了其他设置,但没有运气,但不是全部。

任何具体的建议将不胜感激!

原帖

我知道有很多关于这个的话题,但我有一段时间试图让 ScrollView 工作。我正在尝试加载 768x1600 的图像(仅在 iPad 屏幕上),因此您必须垂直滚动才能查看更多图像内容。为了创建 ScrollView,我使用 Storyboard 并将 ScrollView 拖到 ViewController 框架中,对齐它并确保启用交互。然后,在 ScrollView 之上(也在 Storyboard 中)我添加了我希望使用的 UIImage(确保它是一个子视图)并相应地调整它的大小,然后也设置启用交互。

无论我多么努力,我都无法让它在模拟器或我的设备上工作。我已经确保我的 ScrollView 在 IB 中设置为委托,即在头文件中有一个 IBOutlet 并且被合成并加载到我的实现文件中。我还确保我已将 contentSize 设置为;

self.scrollView.contentSize = CGSizeMake(768, 1600);

我还尝试通过代码将 UIImage 添加为子视图(而不是在情节提要上),当我这样做时,运行模拟器时图像不会出现。最重要的是,当我单击页面时,没有可用的滚动选项。

任何人都可以在这方面给我任何帮助,我们将不胜感激。如果需要代码,我会发布它。

4

1 回答 1

2

我想我刚刚想通了。你说你在滚动视图上给 UIImageView 下了药。

UIImageView 不是用户可交互的!

尝试在 Interface Builder 中选择您的图像视图,并将图像视图的“用户交互启用”设置为“是”。

如果这不起作用,我建议在代码中添加图像视图(首先从 IB 中删除):

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage.png"]];
    self.imageView.frame = CGRectMake(0,0,768,1600);
    self.imageView.userInteractionEnable = YES;
    [self.scrollView addSubview:self.imageView];
    self.scrollView.contentSize = CGSizeMake(768, 1600);
    self.scrollView.frame = self.view.bounds;
}

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

@end
于 2013-05-10T04:08:28.027 回答