在iOS单视图应用中,何时是self.view.bounds设置正确的最早时间? [英] On an iOS single view app, when is the earliest time that self.view.bounds is set correctly?

查看:86
本文介绍了在iOS单视图应用中,何时是self.view.bounds设置正确的最早时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加 UITableView 对象,以掩盖整个屏幕,

  UITableView * tabelView = [[UITableView alloc] initWithFrame:self.view.bounds]; 
[self.view addSubview:tabelView];

但我意识到即使物理iPad 2旋转到横向模式, self.view.bounds {$ 0,0},{768,1004}} in viewDidLoad viewWillAppear 。仅在 viewDidAppear 中最终 {{0,0},{1024,748}}



self.view.bounds 的最早时间是否正确设置为 {{0,0}} ,{1024,748}} ?如果 viewDidAppear 是那个,那么我必须在那里创建 UITableView ,如果启动图像是静态横向空白表,当调用 viewDidAppear 时,屏幕将闪烁白色屏幕,然后添加并看到该表,因此可以看到一个白色闪烁的屏幕。 / p>

因此, self.view.bounds 的最早时间是否正确设置为 { {0,0},{1024,748}} ?看起来它需要稍微在 viewDidAppear 之前,以便绘制空白表(带有灰色线条),并显示它并无缝显示以掩盖启动图片。如果表是在 viewDidLoad viewWillAppear 中使用 self.view.bounds ,然后该表最终为768点宽,适用于肖像模式。



更新:我刚刚尝试添加启动图像由于某种原因,即使在 viewDidAppear 中添加 UITableView ,白屏闪烁也没有发生...我不知道为什么,但之前如果我使用 CALayer 和位图来显示内容,并且没有使用任何视图或 drawRect ,然后发生了闪光......顺便说一句,我找到的最早的是 viewWillLayoutSubviews ,然后是 viewDidLayoutSubviews ,两者都发生在 viewDidAppear 之前,并且都显示 self.view.bounds as {{0,0},{1024,748}}



更新2:如果视图复杂且耗时,该怎么办?为了测试,我在 tableView:cellForRowAtIndexPath 中添加了一个 usleep(100000); 来为每个单元格休眠0.1秒,确实发生了闪光...但是细胞和桌子据说应该重量轻,速度快,但是如果还有其他类型的视图需要更长时间的制作呢?如果我将 UITableView 创建移动到 viewWillLayoutSubviews ,那么显示内容仍然很慢,但至少没有闪。但是,稍后当我旋转设备时,它实际上再次调用 viewWillLayoutSubviews 并将另一个表添加到主视图(具有正确的屏幕大小),所以 viewWillLayoutSubviews 首先需要删除任何旧的表格视图,如果有的话,或者只是调整旧的表格视图,而不是添加新的。

解决方案

避免闪存的最简单方法是在 viewDidLoad 中创建表视图并正确设置表视图的自动调整掩码,这样系统会自动使表视图填充其超级视图:

   - (void)viewDidLoad {
[super viewDidLoad ]。
UITableView * tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:tableView];
}

否则,您可以看到最终视图边界的最早时间是在查看控制器的 viewWillLayoutSubviews 方法。您可以在 viewDidLoad 中添加一次表视图,然后在 viewWillLayoutSubviews 或之后调用的方法中调整其帧。有关更多详细信息,请参阅我对 UIViewController返回无效框架的答案?的回答。


I am trying to add a UITableView object that covers up the whole screen, by

  UITableView *tabelView = [[UITableView alloc] initWithFrame:self.view.bounds];
  [self.view addSubview:tabelView];

but I realized that even if a physical iPad 2 is rotated to Landscape mode, self.view.bounds is {{0, 0}, {768, 1004}} in viewDidLoad and viewWillAppear. Only in viewDidAppear is it finally {{0, 0}, {1024, 748}}

What is the earliest time that self.view.bounds is set correctly to {{0, 0}, {1024, 748}}? If viewDidAppear is the one, then I have to create the UITableView there, and if the "Launch image" is a static Landscape blank table, then the screen will flash a white screen when viewDidAppear is called, and then the table is added and seen, so a flash of white screen is seen.

So when is the earliest time that self.view.bounds is set correctly to {{0, 0}, {1024, 748}}? It seems like it needs to be slightly before viewDidAppear, so that the blank table is drawn (with the grey lines), and show it and seamlessly get displayed to cover up the Launch image. If the table is created in viewDidLoad or viewWillAppear using self.view.bounds, then the table ends up being 768 points wide and is meant for the Portrait mode.

Update: I just tried to add the Launch image and for some reason, even if the UITableView is added in viewDidAppear, the "flash of white screen" didn't happen... I don't know why, but previously if I use CALayer and bitmap to show content, and didn't use any view or drawRect, then the flash occurred... by the way, the earliest one I found was viewWillLayoutSubviews, and then later in viewDidLayoutSubviews, both of which happened before viewDidAppear, and both showed self.view.bounds as {{0, 0}, {1024, 748}}

Update 2: What if the view is complicated and time consuming to make? To test, I added a usleep(100000); in the tableView:cellForRowAtIndexPath to sleep 0.1 second for each cell, and the "flash" did occur... but cell and table supposedly should be light weight and fast to make, but what if there are other type of views that are more time consuming to make? If I move UITableView creation to viewWillLayoutSubviews, then it is still slow to show something, but at least there is no "flash". However, later on when I rotate the device, it actually call viewWillLayoutSubviews again and add another table to the main view (with correct screen size), so viewWillLayoutSubviews will need to remove any old table view first, if any, or just resize the old one and not add a new one.

解决方案

The simplest way to avoid the flash is to create the table view in viewDidLoad and set the table view's autoresizing mask properly, so that the system automatically makes the table view fill its superview:

- (void)viewDidLoad {
    [super viewDidLoad];
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:tableView];
}

Otherwise, the earliest time that you can see the final view bounds are in the view controller's viewWillLayoutSubviews method. You would add your table view once in viewDidLoad, and then adjust its frame in viewWillLayoutSubviews or a method called after that. See my answer to UIViewController returns invalid frame? for more details.

这篇关于在iOS单视图应用中,何时是self.view.bounds设置正确的最早时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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