首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIToolbar UIWebView

UIToolbar UIWebView
EN

Stack Overflow用户
提问于 2015-07-20 02:16:32
回答 1查看 1.1K关注 0票数 0

我正在尝试将工具栏添加到UIWebView上。我有一个表格视图,当一行被选中时,它会推送一个UIWebView,在那个web视图中,我想要一个工具栏,我可以在其中添加前进和后退按钮。我正在阅读一本关于iOS编程的书,这就是他们要求完成练习的方式。到目前为止,我还不能让工具栏真正显示在WebView中,所以我的问题是,在让工具栏显示时,我做错了什么?下面是我的UIWebViewController.m中的代码

代码语言:javascript
复制
UIWebView *webView = [[UIWebView alloc] init];
webView.scalesPageToFit = YES;
self.view = webView;

CGRect frame = CGRectMake(0, 0, 360,44);

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame: frame];


UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:@selector(goBack)];

NSArray *items = [[NSArray alloc] initWithObjects:back, nil];

[toolBar setItems:items animated:YES];

[self.view addSubview:toolBar];
[self.view bringSubviewToFront:toolBar];

当我运行此命令时,工具栏不显示。我已经尝试了很多不同的配置来定位实际的CGRect框架,但没有。更让我困惑的是,如果我使用相同的代码(对工具栏视图进行一些修改并将其放入测试项目中的应用程序委托中,工具栏就会显示出来。

代码语言:javascript
复制
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor whiteColor];

[self.window makeKeyAndVisible];

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 44)];

toolBar.barStyle = UIBarStyleDefault;

UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:self action:nil];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(goBack)];
UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:@selector(goForward)];

NSMutableArray *items = [[NSMutableArray alloc] init];

[items addObject:space];
[items addObject:back];
[items addObject:forward];

[toolBar setItems:items animated:YES];


[self.window addSubview:toolBar];

return YES;
EN

回答 1

Stack Overflow用户

发布于 2015-07-20 02:27:55

就创建UIToolbar而言,代码看起来还不错,但我认为在您创建它的时候,self.view可能并不存在,或者self.window在那一刻仍然是空的。

确保在viewWillAppearviewDidLoad方法中调用代码,此时self.viewself.window都存在。

===编辑===

我认为另一个问题是您将其添加为UIWebView的子视图。尝试将UIWebView添加为视图的子视图。

代码语言:javascript
复制
[self.view addSubview:webView]

如果你从nib加载东西,你这样做似乎是错误的,你不应该以这种方式覆盖view

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31504387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档