如何在超级视图中完全限制子视图(这是一个UIImageView)?

时间:2014-04-26 13:25:56

标签: ios objective-c uiscrollview uiimageview uiimage

我想在滚动视图中显示照片。因为我想向用户显示整张照片,然后允许用户捏合照片进行放大和缩小。首先,我将contentsize设置为固定大小(例如(320,568))。但是,当我添加UIImageView作为滚动视图的子视图时,如果UIImageView的大小的图像大于滚动视图的内容大小,则仅显示图像的上部和左部。所以我想知道如何在scrollview中限制imageview并设置好图像(就像大多数应用程序中的照片一样)。我知道一个方法:clipsToBounds,我使用它。但它不起作用。谁能帮我?这是我的简单代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.photoScrollView.contentSize = CGSizeMake(320, 568);
    self.photoScrollView.clipsToBounds = YES;

    UIImage *image = [UIImage imageNamed:@"image"];
    UIImageView *iv = [[UIImageView alloc] initWithImage:image];

    [self.photoScrollView addSubview:iv];

    [iv sizeToFit];
    iv.contentMode = UIViewContentModeScaleAspectFit;

}

提前致谢!

1 个答案:

答案 0 :(得分:0)

    - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.photoScrollView.contentSize = CGSizeMake(320, 568);
        self.photoScrollView.clipsToBounds = YES;

        self.photoScrollView.maximumZoomScale = 3.0;
        self.photoScrollView.minimumZoomScale = 1.0;

        UIImage *image = [UIImage imageNamed:@"image"];
        UIImageView *iv = [[UIImageView alloc] initWithImage:image];

        [self.photoScrollView addSubview:iv];


        iv.contentMode = UIViewContentModeScaleAspectFit;
        iv.frame=CGRectMake(0, 0, 320, 568); // update this line
// Declare UIImageView* imgView property then assign self.imgView=iv;

    }

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
{
    return self.imgView;
}