【问题标题】:How to prevent UIView from being resized to fit ScrollView height (autoresizing disabled)?如何防止 UIView 被调整大小以适应 ScrollView 高度(禁用自动调整大小)?
【发布时间】:2011-10-26 08:56:14
【问题描述】:

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

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

EDIT 如果有帮助,我会添加一些。在 ViewController 中,我创建了一个带有以下选项的滚动视图:

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 文件吗?
  • 是的,但是有点迟钝。希望我能尽快回到这个应用程序并做好。

标签: ios xcode uiview


【解决方案1】:

在类ReaderContentView中的函数updateMinimumMaximumZoom中:

适合屏幕的缩放比例的计算是在单视图中完成的,但在横向中它应该从容器视图中计算出来。

尝试替换此代码

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

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

【讨论】:

  • 在横向双页模式下,对于同一个项目(vfr / reader pdf),如何在滑动完成后进入下一页。例如,我在第 1 页,并且我在 theContainerView 中显示了 2 个页面,当我滑动时,我想加载第 3 页和第 4 页。目前,当我进行滑动时,它会显示第 2 页和第 3 页。如何实现它?
【解决方案2】:

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

【讨论】:

  • 不确定需要哪些代码,但在我最初的帖子中添加了很多
  • 当然,您不需要第二个滚动视图。并修复它:self.autoresizesSubviews = YES; 我在帖子中看到“每个视图的 autoresizingMask 设置为 UIViewAutoresizingNone”,在代码中看到 theScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;(以及其他一些视图)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-16
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多