如何防止UIView调整大小以适应ScrollView高度(禁用自动调整大小)? [英] How to prevent UIView from being resized to fit ScrollView height (autoresizing disabled)?

查看:81
本文介绍了如何防止UIView调整大小以适应ScrollView高度(禁用自动调整大小)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vfr-reader库编写PDF阅读器。要在横向中显示两个页面,我将每个页面呈现为自己的视图,然后将这两个视图添加到容器视图,然后将容器视图添加到滚动视图。每个视图的autoresizingMask设置为UIViewAutoresizingNone,contentMode设置为UIViewContentModeRedraw,autoresizingSubviews设置为所有视图的NO。

I'm writing a PDF reader using vfr-reader library. To display two pages in landscape i'm rendering each page into its own view, then add these two views to a container view, then add container view to a scroll view. Each view's autoresizingMask is set to UIViewAutoresizingNone, contentMode is UIViewContentModeRedraw, autoresizingSubviews is set to 'NO' for all views.

但仍然以某种方式将容器视图自动调整以适应滚动视图的高度,我不知道这是在哪里发生的。我关心这个,因为,当自动化容器视图时,它的宽度变得比屏幕宽度大,并且我无法通过单次滑动滚动到接下来的两个页面(需要两次滑动),这很糟糕。我错过了什么?

But still somehow container view is autoresized to fit scroll view's height, and I don't know where is this happening. I care about this because, when autoresizing container view, it's width becomes larger than screen width, and I cannot scroll to the next two pages with a single swipe (two swipes are needed), which sucks. What am I missing?

EDIT 如果它有帮助的话会添加一些来。在ViewController中,我创建了一个Scroll View,其中包含以下选项:

EDIT gonna add some come if it helps. In the ViewController I create a Scroll View with following options:

theScrollView = [[ReaderScrollView alloc] initWithFrame:viewRect];
theScrollView.scrollsToTop = NO;
theScrollView.pagingEnabled = YES;
theScrollView.delaysContentTouches = NO;
theScrollView.showsVerticalScrollIndicator = NO;
theScrollView.showsHorizontalScrollIndicator = NO;
theScrollView.contentMode = UIViewContentModeRedraw;
theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
theScrollView.backgroundColor = [UIColor clearColor];
theScrollView.userInteractionEnabled = YES;
theScrollView.autoresizesSubviews = NO;
theScrollView.delegate = self;
[self.view addSubview:theScrollView];

当我正在绘制页面时,我正在向Scroll View添加一个UIView,它正在启动这样:

When I'm drawing pages I'm adding an UIView to the Scroll View, which is being initiated this way:

if ((self = [super initWithFrame:frame]))
{
    self.autoresizesSubviews = YES;
    self.userInteractionEnabled = YES;
    self.contentMode = UIViewContentModeRedraw;
    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;       
    self.backgroundColor = [UIColor clearColor];

    theScrollView = [[ReaderScrollView alloc] initWithFrame:self.bounds]; // Not sure about this part - why is the 2nd scroll view added?
// this is the way its done in vfr reader

    theScrollView.scrollsToTop = NO;
    theScrollView.delaysContentTouches = NO;
    theScrollView.showsVerticalScrollIndicator = NO;
    theScrollView.showsHorizontalScrollIndicator = NO;
    theScrollView.contentMode = UIViewContentModeRedraw;
    theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    theScrollView.backgroundColor = [UIColor clearColor];
    theScrollView.userInteractionEnabled = YES;
    theScrollView.autoresizesSubviews = NO;
    theScrollView.bouncesZoom = YES;
    theScrollView.delegate = self;

    theContentView = [[ReaderContentPage alloc] initWithURL:fileURL page:page password:phrase landscape:(BOOL)isLandscape position:FALSE];
    CGRect viewRect = CGRectZero;
    viewRect.size.width = theContentView.bounds.size.width;
    viewRect.size.height = theContentView.bounds.size.height;    


    if( isLandscape){
        NSLog(@"Landscape detected in content view");
        if (theContentView == NULL)
        {
            theContentView = [[ReaderContentPage alloc] initWithURL:fileURL page:(page+1) password:phrase landscape:(BOOL)isLandscape position:FALSE];
            theContentView2 = NULL;
            viewRect.size.width = theContentView.bounds.size.width;
            viewRect.size.height = theContentView.bounds.size.height;    
        } else {
            if (page == 1)
                theContentView2 = NULL;
            else
                theContentView2 = [[ReaderContentPage alloc] initWithURL:fileURL page:(page+1) password:phrase landscape:(BOOL)isLandscape position:TRUE];
            if (theContentView2 != NULL)
               viewRect.size.width = theContentView.bounds.size.width*2;
        }            

    }       
    if (theContentView != nil) // Must have a valid and initialized content view
    {

        theContainerView = [[UIView alloc] initWithFrame:viewRect];


        theContainerView.autoresizesSubviews = NO;
        theContainerView.userInteractionEnabled = NO;
        theContainerView.contentMode = UIViewContentModeRedraw;            
        theContainerView.autoresizingMask = UIViewAutoresizingNone;
        theContainerView.backgroundColor = [UIColor whiteColor];


        theScrollView.contentSize = theContentView.bounds.size; // Content size same as view size


        theScrollView.contentOffset = CGPointMake((0.0f - CONTENT_INSET), (0.0f - CONTENT_INSET));
        theScrollView.contentInset = UIEdgeInsetsMake(CONTENT_INSET, CONTENT_INSET, CONTENT_INSET, CONTENT_INSET);

        theThumbView = [[ReaderContentThumb alloc] initWithFrame:theContentView.bounds]; // Page thumb view           

        [theContainerView addSubview:theThumbView]; // Add the thumb view to the container view

        [theContainerView addSubview:theContentView]; // Add the content view to the container view

        if(( isLandscape) && (theContentView2 != NULL)){            
             [theContainerView addSubview:theContentView2]; // Add the content view to the container view

        }           



        [theScrollView addSubview:theContainerView]; // Add the container view to the scroll view

        [self updateMinimumMaximumZoom]; // Update the minimum and maximum zoom scales

        theScrollView.zoomScale = theScrollView.minimumZoomScale; // Zoom to fit
    }

    [self addSubview:theScrollView]; // Add the scroll view to the parent container view   

    [theScrollView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:NULL];

    self.tag = page; // Tag the view with the page number
}

return self;

以这种方式创建ReaderContentPage:

And ReaderContentPage is created this way:

if ((self = [super initWithFrame:frame]))
    {
        self.autoresizesSubviews = NO;

        self.userInteractionEnabled = NO;
        self.clearsContextBeforeDrawing = NO;
        self.contentMode = UIViewContentModeRedraw;     
        self.autoresizingMask = UIViewAutoresizingNone;
        self.backgroundColor = [UIColor clearColor];

        view = self; // Return self
    }


推荐答案

在类中的函数 updateMinimumMaximumZoom ReaderContentView

计算适合屏幕的缩放比例是用单一视图完成的,但在横向上它应该从theContainerView计算。

The calculation of the zoomscale to fit the screen is done with single view, but in landscape it should be calculated form theContainerView.

尝试替换此代码

CGFloat zoomScale = ZoomScaleThatFits(targetRect.size,theContentView.bounds.size);

with

CGFloat zoomScale = ZoomScaleThatFits(targetRect.size,theContainerView.bounds.size);

这篇关于如何防止UIView调整大小以适应ScrollView高度(禁用自动调整大小)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆