[英]Why does adding subview in viewDidLoad doesn't work
I was trying to add UIToolBar into a UIWebView but whenever I insert this code inside viewDidLoad UIToolBar doesn't show up, however when this is done in viewWillAppear, it works. 我试图将UIToolBar添加到UIWebView中,但是每当我在viewDidLoad中插入此代码时,UIToolBar都不会显示,但是在viewWillAppear中完成此操作后,它会起作用。 Can someone please explain it to me.
有人可以向我解释一下。
-(void)viewDidLoad
{
[super viewDidLoad];
self.forwardButton = [[UIBarButtonItem alloc]initWithTitle:@"Forward" style:UIBarButtonItemStylePlain target:self action:@selector(forward)];
self.backButton = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
NSArray *buttonsArray = @[self.backButton,self.forwardButton];
CGRect toolBarFrame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
self.toolbar = [[UIToolbar alloc]initWithFrame:toolBarFrame];
[self.toolbar setItems:buttonsArray animated:YES];
[self.toolbar sizeToFit];
[self.view addSubview:self.toolbar];
}
Add subview in viewDidLoad
, but set its frame
in viewWillAppear
. 在
viewDidLoad
添加子视图,但在viewWillAppear
设置其frame
。 self.view.frame
is being set just before viewWillAppear
is called by UIKit. self.view.frame
被设定之前viewWillAppear
被称为的UIKit。 The value you get in viewDidLoad
is the one was set in nib and is often different from the actual frame
. 在
viewDidLoad
获得的值是在nib中设置的值,通常与实际frame
不同。
Don't forget to set autoresizingMask
or use auto layout. 不要忘记设置
autoresizingMask
或使用自动布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.