几年来我一直这样做的方式是简单地创建两个工具栏,并在加载 UIWebView 时将一个或另一个设置为隐藏。我所有的工具栏和 barbutton 项目都是在 IB 中创建的。如果我可以在没有这种“黑客”的情况下使我的实现更小,我感谢提供的任何帮助。
这是我正在使用的代码:
#import "Web.h"
@interface Web()
@property (nonatomic, strong) IBOutlet UIWebView *webView;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *refreshButton;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *stopButton;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *backButtonRefreshToolbar;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *forwardButtonRefreshToolbar;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *backButtonStopToolbar;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *forwardButtonStopToolbar;
@property (nonatomic, strong) IBOutlet UIToolbar *toolbarStop;
@property (nonatomic, strong) IBOutlet UIToolbar *toolbarRefresh;
@end
@implementation Web
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.toolbarStop.hidden = NO;
self.toolbarRefresh.hidden = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.toolbarStop.hidden = YES;
self.toolbarRefresh.hidden = NO;
[self actualizeButtons];
}
- (void)actualizeButtons
{
self.backButtonRefreshToolbar.enabled = [self.webView canGoBack];
self.forwardButtonRefreshToolbar.enabled = [self.webView canGoForward];
self.backButtonStopToolbar.enabled = [self.webView canGoBack];
self.forwardButtonStopToolbar.enabled = [self.webView canGoForward];
}