ios UIScrollView setContentSize 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20530388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIScrollView setContentSize doesn't work
提问by anthoprotic
I have a really big problem with UIScrollView. To make sure it wasn't my actual project, I created a NEW project, iPhone only, for iOS 6 and 7. I disabled autolayout.
UIScrollView 有一个非常大的问题。为了确保它不是我的实际项目,我为 iOS 6 和 7 创建了一个新项目,仅限 iPhone。我禁用了 autolayout。
- I created a Tab Bar project
- I created a view controller, embedded it in a navigation controller and connected it as Tab bar item #0
- I put a UIScrollView in my view.
- I connected the scrollview in the custom UIViewController created for this view.
- I synthesized my IBOutlet and set the delegate to self. I also implemented delegate in .h
- 我创建了一个 Tab Bar 项目
- 我创建了一个视图控制器,将其嵌入到导航控制器中并将其连接为 Tab bar item #0
- 我在我的视图中放置了一个 UIScrollView。
- 我在为此视图创建的自定义 UIViewController 中连接了滚动视图。
- 我合成了我的 IBOutlet 并将委托设置为 self。我也在 .h 中实现了委托
Here is my code (ViewDidLoad):
这是我的代码(ViewDidLoad):
self.scrollView.delegate = self;
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)];
NSLog(@"contentSize width %f", self.scrollView.contentSize.width);
NSLog(@"contentSize height %f", self.scrollView.contentSize.height);
NSLog(@"%@", NSStringFromCGAffineTransform(self.scrollView.transform));
It returns me "contentSize width 20000.000000", "contentSize height 0.000000 " and "[1, 0, 0, 1, 0, 0]". It scrolls left-right where it should scroll up-down
它返回我“contentSize width 20000.000000”、“contentSize height 0.000000”和“[1, 0, 0, 1, 0, 0]”。它在应该上下滚动的地方左右滚动
Please help!
请帮忙!
回答by mr.sosa
I had the same issue until I realized I hadn't set the UIScrollView delegate. Also, if you're using auto layout, you need to wait for the layout to be applied to the UIScrollView. This means you should set the Content Size in "viewDidLayoutSubviews".
我遇到了同样的问题,直到我意识到我没有设置 UIScrollView 委托。此外,如果您使用自动布局,则需要等待布局应用于 UIScrollView。这意味着您应该在“viewDidLayoutSubviews”中设置内容大小。
回答by quaertym
In viewDidLoad method, frame may not be correct, move the code to this method:
在 viewDidLoad 方法中,frame 可能不正确,将代码移至该方法:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Your code
}
回答by Karen Hovhannisyan
Use this code. ScrollView setContentSize should be called async in main thread.
使用此代码。ScrollView setContentSize 应在主线程中异步调用。
Swift:
迅速:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
DispatchQueue.main.async {
var contentRect = CGRect.zero
for view in self.scrollView.subviews {
contentRect = contentRect.union(view.frame)
}
self.scrollView.contentSize = contentRect.size
}
}
Objective C:
目标 C:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
dispatch_async(dispatch_get_main_queue(), ^ {
CGRect contentRect = CGRectZero;
for(UIView *view in scrollView.subviews)
contentRect = CGRectUnion(contentRect,view.frame);
scrollView.contentSize = contentRect.size;
});
}
回答by JustAnotherCoder
Okay, there is absolutely nothing wrong with your code. So some suggestions:
好的,您的代码绝对没有问题。所以一些建议:
Make sure you added to your .h file like so:
@interface yourViewController : UIViewController <UIScrollViewDelegate> { }
Try moving this snippet of code to the "viewWillLoad" method:
- (void)viewWillAppear:(BOOL)animated { self.scrollView.delegate = self; [self.scrollView setScrollEnabled:YES]; [self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)]; }
确保您像这样添加到 .h 文件中:
@interface yourViewController : UIViewController <UIScrollViewDelegate> { }
尝试将此代码片段移至“viewWillLoad”方法:
- (void)viewWillAppear:(BOOL)animated { self.scrollView.delegate = self; [self.scrollView setScrollEnabled:YES]; [self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)]; }
回答by anuraagdjain
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var contentRect = CGRect.zero
for view: UIView in scrollView.subviews {
if !view.isHidden {
contentRect = contentRect.union(view.frame)
}
}
scrollView.contentSize = contentRect.size
scrollView.contentSize.width = self.view.frame.width
}
Working Swift 3.X code. Converted @Karen Hovhannisyan's answer.
工作 Swift 3.X 代码。转换了@Karen Hovhannisyan 的答案。