我的toolBar不会显示在webView上方

时间:2014-03-21 19:11:22

标签: ios iphone objective-c uiviewcontroller uiwebview

我正在尝试向我的viewcontroller添加工具栏。

我的viewController:

-(void)loadView
{
    UIWebView *webView = [[UIWebView alloc]init];
    webView.scalesPageToFit = YES;
    self.view = webView;

    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.frame = CGRectMake(0,self.view.bounds.size.height-50, self.view.bounds.size.width, 50);
    UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"Send" style:UIBarButtonItemStyleDone target:self action:@selector(sendAction)];

    UIBarButtonItem *button2=[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleDone target:self action:@selector(cancelAction)];

    [toolbar setItems:[[NSArray alloc] initWithObjects:button1, nil]];
    [toolbar setItems:[[NSArray alloc] initWithObjects:button2, nil]];
    [self.view addSubview:toolbar];
}

工具栏不会显示。

我应该避免:

 self.view = webView;

只需设置self.view的框架,只需将webView添加为子视图即可?

1 个答案:

答案 0 :(得分:2)

你在这一行中有一个错误的估计:

toolbar.frame = CGRectMake(0,self.view.bounds.size.height-50, self.view.bounds.size.width, 50);

您已将视图的Y位置设置为比视图高度小50个单位。这可行,但此时self.view.frame尚未正确计算并包含(0,0,0,0)rect。最好使用AutoLayout,但简单/ hacky解决方案如下:

CGRect mainScreenFrame = [[UIScreen mainScreen] applicationFrame];
toolbar.frame = CGRectMake(0,mainScreenFrame.size.height-50, self.view.bounds.size.width, 50);