手动设置约束时,UIImageView不会缩小图像

时间:2014-05-09 19:17:37

标签: ios objective-c uiscrollview uiimageview

我在UICollectionView内有UITableViewCell,而CollectionView单元格显示用户在应用中存储的图像。当用户点击单元格时,图像将全屏显示。因此,我创建了UIViewController,为其添加了UIScrollView进行缩放,然后向ScrollView添加了UIImageView。在用户改变手机的方向之前,一切都很顺利。当它们更改为横向时,视图会调整,但滚动视图和图像视图不会调整,因此只显示在屏幕的左侧;他们似乎保持相同的界限。我尝试使用纯布局方法并添加约束来遵循Apple的技术说明来解决这个问题。但是当我这样做时,显示的图像会一直缩放,我无法正确显示。当我使用NSLog语句来确定帧时,它们都被设置为self.view.frame,因此打印出屏幕大小,但图像仍然以完整大小显示。一切都是以编程方式创建的,没有xib文件。

我希望避免这些限制,但似乎用户必须能够在横向模式下正确地看到图像。我已经尝试设置scrollview的contentSize,将clipsToBounds设置为YES并更改contentMode,但这些都没有任何效果。非常感谢任何帮助!

collectionView:didSelectItemAtIndexPath:

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *viewsDictionary;

    //creating ViewController to be presented
    UIViewController *fullImageView = [[UIViewController alloc] init];
    fullImageView.view.userInteractionEnabled = YES;
    fullImageView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    _selectedImageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    NSString *key = [self.viewItem.imageKeyArray objectAtIndex:indexPath.row];
    _selectedImageView.image = [self.viewItem.itemImages objectForKey:key];
    _selectedImageView.clipsToBounds = YES;
    _selectedImageView.contentMode = UIViewContentModeScaleAspectFit;
    _selectedImageView.translatesAutoresizingMaskIntoConstraints = NO;


    UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    imageScrollView.delegate = self;
    imageScrollView.minimumZoomScale = 1;
    imageScrollView.maximumZoomScale = 2;
    imageScrollView.translatesAutoresizingMaskIntoConstraints = NO;

    [fullImageView.view addSubview:imageScrollView];
    [imageScrollView addSubview:_selectedImageView];


    viewsDictionary = NSDictionaryOfVariableBindings(imageScrollView, _selectedImageView);
    [fullImageView.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[imageScrollView]|" options:0 metrics: 0 views:viewsDictionary]];
    [fullImageView.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[imageScrollView]|" options:0 metrics: 0 views:viewsDictionary]];
    [imageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_selectedImageView]|" options:0 metrics: 0 views:viewsDictionary]];
    [imageScrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_selectedImageView]|" options:0 metrics: 0 views:viewsDictionary]];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissFullImageView)];
    [fullImageView.view addGestureRecognizer:tap];


    [self presentViewController:fullImageView animated:YES completion:nil];
}

0 个答案:

没有答案