[英]Adjust scroll view indents doesn't work if there is a view below the scrollview
我正在Xcode 5.0.1的情節提要中制作一個適用於iOS 7的應用程序。
我有一個帶有滾動視圖的視圖控制器,該視圖顯示在導航控制器內部,底部帶有工具欄。
滾動視圖可以正確插入,並在啟用“調整滾動視圖插入”的情況下在頂部和底部欄下正確滾動。
我想添加一個不會在滾動視圖后面滾動的背景圖像。 當我這樣做時,就好像未選中Adjusts Scroll View Insets標志一樣,滾動條和內容始終位於頂部和底部。
不管是將視圖放入情節提要中,還是將其以編程方式在ViewDidLoad或ViewWillAppear中進行設置,還是將其放在情節提要中的較高位置,然后以編程方式將其移回至后面,都可以同樣的結果,滾動條不調整條形。
如果我將其加載或移動到viewDidAppear中,則它可以正常工作,但是對於我的過渡來說為時已晚,我需要在它開始變得可見時在后台顯示該視圖。
我懷疑這是ios7的錯誤,是否有任何已知的解決方法?
我遇到了同樣的問題,解決方案並不簡單。 我所做的是將UIScrollView子類化,向其中添加圖像的子層並將其與滾動一起移動。 我敢肯定,有更好的方法可以做我所做的事情,但是它可以工作。
這是ScrollViewWithBackgroundImage.m。 我不使用arc,如果要使用的話,只需刪除dealloc方法,然后將“ retain”更改為“ strong”。 另外,如果您尚未添加#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
和ScrollViewWithBackgroundImage.h
@interface ScrollViewWithBackgroundImage : UIScrollView
- (void)setBackgroundImage:(UIImage *)image;
@end
然后,在視圖控制器中:
- (void)viewDidLoad {
[super viewDidLoad];
// The rest of your viewDidLoad method...
[self.scrollView setBackgroundImage:[UIImage imageNamed:@"bk.default.png"]];
}
編輯:問題的原因是,只有滾動視圖是堆棧中的第一個子視圖時, automaticallyAdjustScrollViewInsets
屬性才會起作用。 您可以將滾動視圖嵌入視圖中,只要滾動視圖在堆棧的底部(在界面構建器的頂部),它仍將起作用。 每當您在滾動視圖下方添加另一個子視圖時,自動插入調整將停止工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.