简体   繁体   中英

How do I vertically center a UIImageView in a UIScrollView programmatically?

I have a view controller that puts a UIImageView inside a UIView wrapper that is then put into a UIScrollView . I need the image to be completely zoomed out and centered vertically.

The View Controller is instantiated and presented within a 600x600 UIPopoverController .

This is the code I have for the view controller:

- (void) loadView
{
    UIImageView *imageView = [[UIImageView alloc]initWithImage:self.image];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:imageView.frame];



    scrollView.delegate = self;
    scrollView.bounces = NO;

    self.containerView = [[UIView alloc] initWithFrame:imageView.frame];


    [self.containerView addSubview:imageView];

    [scrollView addSubview:self.containerView];
    scrollView.minimumZoomScale = .4;
    scrollView.maximumZoomScale = 5.0;
    [scrollView setZoomScale:.4];


    self.view = scrollView;

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

I've tried a bunch of things to center it, but it always appears at the top of the scroll view.

对UIScrollView使用ContentSize和ContentOffSet

使用自动布局并在滚动视图中居中放置容器视图。.如果您需要自动布局帮助,请使用https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/Introduction/Introduction.html此链接。 。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM