无法在viewDidAppear之前正确设置框架 [英] Unable to set frame correctly before viewDidAppear

查看:68
本文介绍了无法在viewDidAppear之前正确设置框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人知道为什么当您在 viewDidLoad viewWillAppear 中设置子视图的框架时不要在屏幕上生效,但如果你在 viewDidAppear 中设置它们呢?

I was wondering if anyone knows why when you set the frame of a subview in viewDidLoad and viewWillAppear the changes do not take affect on the screen, but if you set it in viewDidAppear they do?

在我的情况下我我正在加载一个带有两个tableviews的自定义xib,然后尝试在 viewDidLoad 中将它们向下移动,以便为 viewDidLoad 因为并不总是需要显示它。

In my case I am loading a custom xib with two tableviews then attempting to shift them down in viewDidLoad to allow space for another view which is added in viewDidLoad as it is not always necessary to display it.

问题是我在中设置框架viewDidLoad viewWillAppear 它在对象上设置,我可以通过打印看到它,但它没有反映在屏幕上。将我的设置框架调用移动到 viewDidAppear 将导致一切按预期工作。

The problem is when i set frame in viewDidLoad or viewWillAppear it is set on the object, i can see by printing it out, but it is not reflected on screen. Moving my set frame calls to viewDidAppear will cause everything to work as expected.

认为我应该这样做是错的能够在 viewDidLoad 中设置框架?

Is it wrong to think I should be able to set the frame in viewDidLoad?

- (id)init {
    if ( self = [super initWithNibName:@"MyView" bundle:nil] ) {}

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.descriptionWebView = [[[UIWebView alloc] initWithFrame:CGRectMake( 0, 0, self.view.frame.size.width, 200 )] autorelease];

    [self.view addSubview:self.descriptionWebView];

    self.tableView1.autoresizingMask = UIViewAutoresizingNone;
    self.tableView2.autoresizingMask = UIViewAutoresizingNone;

    [self.descriptionWebView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"...." withExtension:@"html"]]];

    table1LocationWithHeader = CGRectMake( self.tableView1.frame.origin.x, 200, self.tableView1.frame.size.width, self.tableView1.frame.size.height - 200 );
    table2LocationWithHeader = CGRectMake( self.tableView2.frame.origin.x, 200, self.tableView2.frame.size.width, self.tableView2.frame.size.height - 200 );

    //this will NOT work
    self.tableView1.frame = table1LocationWithHeader;
    self.tableView2.frame = table2LocationWithHeader;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    //this will NOT work
    self.tableView1.frame = table1LocationWithHeader;
    self.tableView2.frame = table2LocationWithHeader;
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    //this WILL work
    self.tableView1.frame = table1LocationWithHeader;
    self.tableView2.frame = table2LocationWithHeader;
}


//I added these after comments for stack overflow users, it seems like viewDidLayoutSubviews is the best place to set the frame

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];

    //this will NOT work
    self.tableView1.frame = table1LocationWithHeader;
    self.tableView2.frame = table2LocationWithHeader;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    //this WILL work
    self.tableView1.frame = table1LocationWithHeader;
    self.tableView2.frame = table2LocationWithHeader;
}


推荐答案

在课堂上调用viewDidLoad虽然没有初始化ui元素,因此在viewDidLoad和viewDidAppear调用之间的初始化过程中,任何引用它们的尝试都将被覆盖或不可用。一旦所有ui元素都被初始化并且调用了viewDidAppear,就会调用它。

viewDidLoad is called when the class is loaded however no ui elements have been initialised and therefore any attempt to reference them will be overwritten or unavaliable during the initialisation process which happens between the viewDidLoad and viewDidAppear calls. Once all ui element have been initalised and drawn viewDidAppear is called.


viewDidLoad -
在控制器视图加载到内存后调用

viewDidLoad - Called after the controller's view is loaded into memory

此时视图不在视图层次结构中。

At this point the view isn't within the view hierarchy.


viewWillAppear - 通知视图控制器其视图即将添加到视图层次结构中。

viewWillAppear - Notifies the view controller that its view is about to be added to a view hierarchy.

同样,视图尚未添加到视图层次结构中。

Again, the view is yet to be added to the view hierarchy.


viewDidAppear - 通知视图控制器其视图已添加到视图层次结构中。

viewDidAppear - Notifies the view controller that its view was added to a view hierarchy.

只有这样才能将视图添加到视图层次结构中。

Only then is the view added to the view hierarchy.

更新

viewDidLayoutSubviews 是在UI实际出现在屏幕上之前修改用户界面的最佳位置。

The viewDidLayoutSubviews is the most appropriate place to modify the UI before it actually appears on the screen.


viewDidLayoutSubviews - 通知视图控制器其视图刚刚布置了其子视图。

viewDidLayoutSubviews - Notifies the view controller that its view just laid out its subviews.

这篇关于无法在viewDidAppear之前正确设置框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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