如何防止UIView被调整大小以适应ScrollView高度(禁用自动调整大小)?

5
我正在使用vfr-reader库编写PDF阅读器。为了以横向方式显示两页,我将每页呈现到其自己的视图中,然后将这两个视图添加到一个容器视图中,再将容器视图添加到滚动视图中。对于所有视图,设置了每个视图的autoresizingMask为UIViewAutoresizingNone,contentMode为UIViewContentModeRedraw,将autoresizingSubviews设置为“NO”。
但是,一些情况下容器视图仍会自适应滚动视图的高度,而我不知道它是在哪里发生的。我关心的是当自适应容器视图时,其宽度会变得大于屏幕宽度,且无法通过单次滑动来滚动到下两页(需要两次滑动),这很糟糕。我错过了什么? 编辑:如果有帮助,我将在ViewController中添加一些内容。创建Scroll View并使用以下选项:
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];

当我绘制页面时,我会将一个UIView添加到滚动视图中,这是通过以下方式初始化的:
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 是这样创建的:

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
    }

你在@Spail成功地完成了横向排列的2页PDF吗? - jovhenni19
是的,但有点儿愚蠢。希望我很快能回到这个应用程序并做好它。 - Spail
2个回答

2
在类ReaderContentView的函数updateMinimumMaximumZoom中:
计算适合屏幕的缩放比例是使用单个视图完成的,但在横向模式下应该从theContainerView计算。
尝试用以下代码替换: CGFloat zoomScale = ZoomScaleThatFits(targetRect.size, theContainerView.bounds.size);

在横向双页模式下,对于同一项目(vfr/reader pdf),如何在滑动时切换到下一页?例如,我当前在第一页,在 theContainerView 中显示了两页,当我进行滑动操作时,我想要加载第三页和第四页。但是目前当我进行滑动操作时,它会显示第二页和第三页。如何实现这个功能? - Arunavh Krishnan

1
UIScrollView中有一个contentMode属性,将其更改为UIViewContentModeCenter或其他内容以防止调整大小。

不确定需要哪些代码,但我在初始帖子中添加了很多。 - Spail
当然,您不需要第二个滚动视图。修复它: self.autoresizesSubviews = YES; 我在文章中看到“每个视图的autoresizingMask都设置为UIViewAutoresizingNone”,代码中有 theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;(还有其他一些视图)。 - Pavel Oganesyan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接