我想知道是否有人知道为什么在viewDidLoadviewWillAppear中设置子视图的帧时,更改不会在屏幕上生效,但如果在viewDidAppear中设置,则会生效?

在我的例子中,我正在加载一个带有两个TableView的自定义xib,然后try 在viewDidLoad中向下移动它们,以便为在viewDidLoad中添加的另一个视图留出空间,因为并不总是需要显示它.

问题是,当我在viewDidLoadviewWillAppear中设置帧时,它被设置在对象上,我可以通过打印出来看到,但它不会反映在屏幕上.将我的设置帧调用移动到viewDidAppear将使一切按预期工作.

认为我应该能把相框设置为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和ViewDidAspect调用之间发生的初始化过程中,引用这些元素的任何try 都将被覆盖或不可用.初始化并绘制所有ui元素后,调用ViewDidDisplay.

viewDidLoad-

此时,视图不在视图层次 struct 中.

VIEWWILLEXPEND-通知视图控制器其视图即将添加到视图层次 struct 中.

同样,视图尚未添加到视图层次 struct 中.

ViewDidDisplay-通知视图控制器其视图已添加到视图层次 struct 中.

只有这样,视图才会添加到视图层次 struct 中.

Update

viewDidLayoutSubviews是在UI实际出现在屏幕上之前修改它的最合适位置.

viewDidLayoutSubviews-通知视图控制器其视图刚刚布局了其子视图.

Objective-c相关问答推荐

如何在 NSImage CIFilter ObjC 周围创建边框

iPhone 可达性判断

多个 WKWebView 之间的 Cookie 共享

将未知数量的行添加到静态单元UITableView

Objective-C 对象的列表 Select 器

MKMapView zoom 到 viewDidLoad 上的用户位置?

KVO - 如何判断对象是否是观察者?

iOS中CALayer的UIImage

CABasicAnimation 无 HUGE_VALF 无限重复?

Objective-C 的 Eclipse 插件?

使用 UIViewControllerInteractiveTransitioning 委托协议实现自定义 UIViewController 交互式过渡的正确方法

如何在 XCode 中复制文件?

在 NSMutableArray 的开头添加一个对象?

无法加载从笔尖引用的图像

在Objective-C中替换字符串中的多个字符?

arc4random 和 arc4random_uniform 有什么区别?

如何在 Objective-C 中创建静态方法?

添加/删除带有动画的 UITableViewCell?

计算 UILabel 文本大小

Core Text在iOS中计算字母框架