如何在进入前台时保留UIScrollView的框架? [英] How do I preserve the frame of a UIScrollView upon entering foreground?

查看:93
本文介绍了如何在进入前台时保留UIScrollView的框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题讨论了如何在UIScrollView中的视图之间创建间隙。约定的方法是增加UIScrollView的框架的宽度,以创建将滚动过的视图之间的间隙。但是,当滚动视图是UINavigationController的子视图时,会出现问题。 UINavigationController在 -viewDidLoad 中设置后更改UIScrollView的框架。建议的解决方案是在 -viewDidAppear 中更改框架大小,此时UINavigationController已完成更改操作,更改将保留。

This question discusses how to create gaps between views in a UIScrollView. The agreed method is to increase the width of UIScrollView's frame to create gaps between the views which will be scrolled past. However when the scroll view is a subview of a UINavigationController an issue arises. The UINavigationController changes the frame of the UIScrollView after it has been set up in -viewDidLoad. The solution proposed is to alter the frame size in -viewDidAppear, at which point UINavigationController has finished changing things and the alterations will stick.

- (void)viewDidAppear:(BOOL)animated {
    CGRect frame = self.view.frame;
    frame.size.width += 20; // This gives a 20px gap width
    self.view.frame = frame;
}



我在应用程序中使用过这个文件,但我遇到了问题,当应用程序发送到背景,然后再次检索。看来UINavigationController再次改变框架,将其重置为默认宽度,但这次 -viewDidAppear 没有被调用,因此不会进行更改。我尝试注册 UIApplicationWillEnterForegroundNotification UIApplicationDidBecomeActiveNotification ,但这两个似乎都发生在UINavigationController更改事情之前,框架在这一点没有效果。

I've been using this in my application and it has been working fine, but I run into problems when the app is sent to the background and then retrieved again. It seems that UINavigationController changes the frame once again, resetting it to the default width, but this time -viewDidAppear is not called and so the alteration is never made. I've tried registering for UIApplicationWillEnterForegroundNotification and UIApplicationDidBecomeActiveNotification but both of these seem to occur before UINavigationController changes things, so altering the frame at this point has no effect.

在什么时候我应该尝试改变框架大小,以便它会粘,或者我可以以某种方式阻止UINavigationController改变框架在第一个位置?

At what point should I try to change the frame size such that it will stick, or can I somehow prevent UINavigationController from altering the frame in the first place?

所以我子类化UIScrollView并发现一个方法 [UIViewControllerWrapperView setFrame:] 正在调用,这反过来调用我的子类的 -setFrame:方法设置框架大小。我不知道什么 UIViewControllerWrapperView 是或者,我不知道它会是可能的后,它做了它的东西,并正确设置框架大小。我将继续调查。

So I subclassed UIScrollView and found that a method called [UIViewControllerWrapperView setFrame:] is being called which is in turn calling my subclass's -setFrame: method to set the frame size. I don't know what UIViewControllerWrapperView is or does and I'm not sure it is going to be possible to jump in after it has done its thing and set the frame size properly. I will continue to investigate.

推荐答案

我通过将UIScrollView作为我的View Controller视图的子视图来解决了这个问题。因此,UINavigationController单独保留UIScrollView,并保留框架。这也有额外的好处,允许我应用填充以相同的方法我用来设置滚动视图,而不是后来在 -viewDidLoad

I solved this problem by making the UIScrollView a subview of my View Controller's view. As a result, the UINavigationController leaves the UIScrollView alone and the frame is preserved. This also had the added benefit of allowing me to apply the padding in the same method I use to set up the scroll view, instead of later on in -viewDidLoad.

总而言之,我已经这样做了:

So in summary, I've done this:

CGRect frame = CGRectMake(0, 0, 320, 460);
scrollView = [[UIScrollView alloc] initWithFrame:frame];

// I do some things with frame here

CGRect f = scrollView.frame;
f.size.width += PADDING; // PADDING is defined as 20 elsewhere
scrollView.frame = f;

[self.view addSubview:scrollView];

这篇关于如何在进入前台时保留UIScrollView的框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆