UIScrollView 在视图中设置 contentoffset 将不起作用 [英] UIScrollView Setting contentoffset in viewwillappear not working
问题描述
我想知道是否可以在 viewwillappear 方法中为 uiscrollview 设置 contentoffset.
I was wondering if it is possible to set contentoffset for uiscrollview in viewwillappear method.
-(void) viewWillAppear:(BOOL)animated{
[self.scrollView setContentOffset:CGPointMake(320, 0) animated:YES];
NSLog(@"CALLED");
}
我可以看到 viewwillappear 正在运行,但不幸的是它没有设置偏移量.
I can see viewwillappear is running but unfortunately it is not setting offset.
谢谢
推荐答案
您应该在尝试设置偏移量之前调用 [super viewWillAppear:animated];
.
you should call [super viewWillAppear:animated];
before attempting to set the offset.
但是,您可能试图在视图生命周期中过早地设置偏移量.
However, it is possible you are trying to set the offset too early in the view lifecycle.
最好覆盖 -(void)viewDidLayoutSubviews;
,并在那里设置偏移量.
it might be good to override -(void)viewDidLayoutSubviews;
, and set the offset there.
因为到那时你的视图框架都应该被适当地设置.(记得在那里也调用 super
)
as your view's frames should all be set appropriately by that time. (remember to call super
there too)
这篇关于UIScrollView 在视图中设置 contentoffset 将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!