简体   繁体   English

如果在滚动视图下面有一个视图,则调整滚动视图缩进不起作用

[英]Adjust scroll view indents doesn't work if there is a view below the scrollview

I am working in a storyboard, in Xcode 5.0.1, making an app for iOS 7. 我正在Xcode 5.0.1的情节提要中制作一个适用于iOS 7的应用程序。

I have a view controller with a scroll view being displayed inside a navigation controller with a toolbar on the bottom. 我有一个带有滚动视图的视图控制器,该视图显示在导航控制器内部,底部带有工具栏。

The scrollview insets properly, and scrolls properly under the top and bottom bars with Adjusts Scroll View Insets enabled. 滚动视图可以正确插入,并在启用“调整滚动视图插入”的情况下在顶部和底部栏下正确滚动。

I want to add a background image that does not scroll behind the scroll view. 我想添加一个不会在滚动视图后面滚动的背景图像。 When I do this, it's as if the Adjusts Scroll View Insets flag is unchecked, the scroll bar and the content go behind the top and bottom bars all the time. 当我这样做时,就好像未选中Adjusts Scroll View Insets标志一样,滚动条和内容始终位于顶部和底部。

It doesn't matter if I put the view in the storyboard, or do it programaticaly in ViewDidLoad, or ViewWillAppear, or if I put it in at a higher point in the story board and then move it to the back programatically, I get the same result, the scroll does not adjusts for the bars. 不管是将视图放入情节提要中,还是将其以编程方式在ViewDidLoad或ViewWillAppear中进行设置,还是将其放在情节提要中的较高位置,然后以编程方式将其移回至后面,都可以同样的结果,滚动条不调整条形。

If I load or move it in viewDidAppear then it works, but that's too late for my transitions, I need the view in the background by the time it begins to become visible. 如果我将其加载或移动到viewDidAppear中,则它可以正常工作,但是对于我的过渡来说为时已晚,我需要在它开始变得可见时在后台显示该视图。

I suspect this is a bug with ios7, are there any known workarounds? 我怀疑这是ios7的错误,是否有任何已知的解决方法?

I faced the same problem and the solution is not straightforward. 我遇到了同样的问题,解决方案并不简单。 What I did was subclass the UIScrollView, add a sublayer with the image to it and move it along with the scroll. 我所做的是将UIScrollView子类化,向其中添加图像的子层并将其与滚动一起移动。 I'm sure there are better ways to do what I did, but it works. 我敢肯定,有更好的方法可以做我所做的事情,但是它可以工作。

This is ScrollViewWithBackgroundImage.m. 这是ScrollViewWithBackgroundImage.m。 I'm not using arc, if you are, just remove the dealloc method and change 'retain' with 'strong'. 我不使用arc,如果要使用的话,只需删除dealloc方法,然后将“ retain”更改为“ strong”。 Also, you will need to add #import <QuartzCore/QuartzCore.h> if you don't do it already. 另外,如果您尚未添加#import <QuartzCore/QuartzCore.h>则还需要添加它。

#import "ScrollViewWithBackgroundImage.h"

@interface ScrollViewWithBackgroundImage()
@property (nonatomic, retain) CALayer *backgroundLayer;
@end

@implementation ScrollViewWithBackgroundImage

- (void)setBackgroundImage:(UIImage *)image {
    CALayer *backgroundImageLayer = [CALayer layer];
    backgroundImageLayer.contents = (id)[image CGImage];
    backgroundImageLayer.bounds = self.bounds;
    backgroundImageLayer.position = self.center;
    [self.layer insertSublayer:backgroundImageLayer atIndex:0];

    self.backgroundLayer = backgroundImageLayer;
    [self setNeedsLayout];
}

-(void)layoutSubviews {
    [super layoutSubviews];

    CGRect frame = self.bounds;
    frame.origin = [self convertPoint:self.bounds.origin toView:self];
    [CATransaction begin];
    [CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];
        self.backgroundLayer.frame = frame;
    [CATransaction commit];
}

- (void)dealloc {
    [_backgroundLayer release];
    [super dealloc];
}

@end

And ScrollViewWithBackgroundImage.h 和ScrollViewWithBackgroundImage.h

@interface ScrollViewWithBackgroundImage : UIScrollView

- (void)setBackgroundImage:(UIImage *)image;

@end

Then, in the view controller: 然后,在视图控制器中:

- (void)viewDidLoad {
    [super viewDidLoad];

    // The rest of your viewDidLoad method...

    [self.scrollView setBackgroundImage:[UIImage imageNamed:@"bk.default.png"]];
}

Edit: The cause of the problem is that automaticallyAdjustScrollViewInsets property will only work if the scroll view is the first subview in the stack. 编辑:问题的原因是,只有滚动视图是堆栈中的第一个子视图时, automaticallyAdjustScrollViewInsets属性才会起作用。 You can embed the scroll view in a view and it will still work as long as the scroll view is at the bottom of the stack (at the top in interface builder). 您可以将滚动视图嵌入视图中,只要滚动视图在堆栈的底部(在界面构建器的顶部),它仍将起作用。 Whenever you add another subview that is below the scrollview the automatic inset adjustment will stop working. 每当您在滚动视图下方添加另一个子视图时,自动插入调整将停止工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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