我正在努力向我的UIWebView的标签栏添加前进和后退按钮,标题似乎不会显示出来。
我初始化它的方式有问题吗?标签栏本身出现,但它是空白的。
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"The url dude is: '%@'", _itemURL);
// Initialize UIWebView
self.myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width,
self.view.frame.size.height)];;
self.myWebView.delegate = self;
[self.view addSubview:self.myWebView];
// Bottom Tab Bar (with back button)
CGRect tabBarFrame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 44);
self.webViewTabBar = [[UITabBar alloc] initWithFrame:tabBarFrame];
UITabBarItem *tabBarItem1 = [self.webViewTabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [self.webViewTabBar.items objectAtIndex:1];
tabBarItem1.title = @"Back";
tabBarItem2.title = @"Forward";
[self.view addSubview:self.webViewTabBar];
// set the url
NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// make url request
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil) {
[self.myWebView loadRequest:request];
[activityIndicator stopAnimating];
}
else if (error != nil) NSLog(@"Error: %@", error);
}];
[self.myWebView setScalesPageToFit:YES];
}
答案 0 :(得分:0)
我几乎不想发布此代码,因为害怕投票(我认为有更好的方法来做到这一点,但我只是不确定它是什么),但它确实可以工作我的所有项目。
这会在屏幕底部添加一个工具栏,其中包含用于向前,向后和重新加载webView的按钮(您必须编写处理这些内容的单独函数)。
UIToolbar *bottomBar = [[UIToolbar alloc]init];
bottomBar.tintColor = [UIColor darkGrayColor];
bottomBar.barTintColor = [UIColor whiteColor];
bottomBar.translucent = NO;
[bottomBar setFrame:CGRectMake(0, self.view.frame.size.height- 196, 320, 44)];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithTitle:@"<" style:UIBarButtonItemStyleBordered target:webView action:@selector(goBack)];
UIBarButtonItem *forward = [[UIBarButtonItem alloc]initWithTitle:@">" style:UIBarButtonItemStyleBordered target:webView action:@selector(goForward)];
UIBarButtonItem *reload = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:webView action:@selector(reload)];
UIBarButtonItem *fake = [[UIBarButtonItem alloc]initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil];
bottomBar.items = [[NSArray alloc]initWithObjects:fake,back,fake,fake, forward,fake,fake,fake,fake,fake,fake, reload, nil];
当然,您必须修改它以满足您的需求,但它确实对我有用。就像我之前说的那样,我相信有更好的方法可以做到这一点,但我不确定那是什么。如果有人能指出更好的方法,我会很高兴。