【发布时间】:2015-07-19 18:16:32
【问题描述】:
我正在尝试将工具栏添加到 UIWebView。 I have a tableview that when a row is selected, it pushes a UIWebView, in that web view I want a toolbar where I can add forward and back buttons.我正在阅读一本关于 iOS 编程的书,这就是他们要求完成练习的方式。到目前为止,我一直无法让工具栏真正显示在 WebView 中,所以我的问题是我在让工具栏显示时做错了什么?这是我的 UIWebViewController.m 中的代码
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 框架定位尝试了很多不同的配置,但事实并非如此。更让我困惑的是,如果我采用相同的代码(对工具栏视图进行一些修改并将其放在测试项目的应用程序委托中,则会显示工具栏。
- (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;
【问题讨论】:
标签: ios objective-c uiwebview uitoolbar