简体   繁体   English

为什么在viewDidLoad中添加子视图不起作用

[英]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.

相关问题 为什么不在viewDidLoad中执行SegueWithIdentifier工作? - Why doesn't performSegueWithIdentifier work inside viewDidLoad? 为什么我的 UIView animation 在我添加子视图后不起作用? - Why does my UIView animation doesn't work after I add a subview to it? 添加子视图后,UITableView动画不起作用 - UITableView animation doesn't work after adding a subview 为什么UITapGestureRecognizer在keyWindow(全屏)的子视图中不起作用? - Why doesn't UITapGestureRecognizer work in subview of keyWindow (full screen)? becomeFirstResponder在viewDidAppear中工作,但在viewDidLoad中不起作用 - becomeFirstResponder works in viewDidAppear but doesn't work in viewDidLoad contentOffset.x 在 vi​​ewDidLoad 中不起作用 - contentOffset.x doesn't work in viewDidLoad 为什么通过添加super.viewdidload()程序不会陷入无限循环? - Why the program doesn't get into an infinite loop by adding super.viewdidload()? 在除viewDidLoad之外的方法中添加子视图 - adding a subview in a method other than viewDidLoad 未显示将子视图添加到UITableViewCell - Adding a SubView to UITableViewCell doesn't get displayed 为什么通过UITabBarController.viewDidLoad中的代码添加的自定义按钮不响应选择器 - why does a custom button which added by code in UITabBarController.viewDidLoad doesn't response the selector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM