【问题标题】:Why can't I add a UIScrollView to a UIImageView (with it working properly)为什么我不能将 UIScrollView 添加到 UIImageView (它工作正常)
【发布时间】:2012-09-27 03:10:43
【问题描述】:

我正在开发一个应用程序。在这里,我描述了我遇到的一个奇怪问题的简化版本。

我有一个情节提要创建的视图控制器,其中 UIImageView 也在情节提要中创建。

然后我有以下 h 和 m 文件,旨在将 UIScrollView 添加到 UIImageview 并为滚动视图提供容器。

h 文件:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic, strong) UIScrollView* scrollView;
@property (nonatomic, strong) UIView* container;

@end

m 文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIImage *image = [UIImage imageNamed: @"new_phots_fake_port.png"];
    //Add the image to the imageView I created in the Storyboard
    [_imageView setImage:image];


    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(60.0, 70.0, 522.0, 100.0)];
    [_scrollView setBackgroundColor:[UIColor yellowColor]];


    // Set up the container view to exist inside the scrollview:
    CGSize containerSize = CGSizeMake(5000.0f, 730.0f);
    _container = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f),       .size=containerSize}];
    _container.backgroundColor = [UIColor redColor];
    // Tell the scroll view the size of the contents
    self.scrollView.contentSize = containerSize;
    [_scrollView addSubview:_container];


    [_imageView addSubview:_scrollView]; //This is what I want but doesn't allow scrolling

    //[self.view addSubview:_scrollView];  //This allows scrolling but makes the imageView      and Scrollview siblings.  I need scrollview to be a child of imageview
   }

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

@end

因此,滚动视图似乎无法添加到图像视图(并且功能正常)。 为什么?我做错了什么?

奇怪的是,我也没有使用任何委托方法 viewForZoomingInScrollView 或 scrollViewDidZoom。我不确定为什么不需要这些。

【问题讨论】:

    标签: objective-c uiscrollview uiimageview storyboard uiscrollviewdelegate


    【解决方案1】:

    你需要在UIImageView上设置userInteractionEnabled

    _imageView.userInteractionEnabled = YES;
    

    【讨论】:

    • 你的回答太快了,Stack Overflow 还不允许我接受你的回答。但是我会。干杯
    猜你喜欢
    • 2017-08-11
    • 1970-01-01
    • 2015-06-29
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多