【问题标题】:My toolBar won't show above the webView我的工具栏不会显示在 webView 上方
【发布时间】:2014-04-29 08:08:53
【问题描述】:

我正在尝试将工具栏添加到我的视图控制器。

我的视图控制器:

-(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 添加为子视图?

【问题讨论】:

    标签: ios iphone objective-c uiviewcontroller uiwebview


    【解决方案1】:

    这行你算错了:

    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) 矩形。最好使用 AutoLayout,但一个简单/hacky 的解决方案如下:

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

    【讨论】:

    • 谢谢,你知道为什么“toolbar.backgroundColor = [UIColor redColor];”不会改变颜色。现在它的颜色很清晰,我只能通过它的按钮识别工具栏:\
    • 那么我的回答解决了问题吗?如果是这样,请接受它作为答案:P。至于颜色问题,试试 [toolbar setTintColor:[UIColor redColor]];您可能还想添加 [toolbar setTranslucent:NO];如果您使用的是 iOS7 并且不想要透明度。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-26
    • 2014-12-16
    • 1970-01-01
    • 2019-02-20
    相关资源
    最近更新 更多