1

几年来我一直这样做的方式是简单地创建两个工具栏,并在加载 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];
}
4

2 回答 2

0

是的。使用单个UIToolbar实例,并在需要交换显示的项目时调用其setItems:animated:方法。animated:NO这应该具有与显示/隐藏一对工具栏相同的视觉效果。

当标题需要更改时,您也可以只设置possibleTitles相关属性。UIBarButtonItem

于 2012-12-01T21:31:40.873 回答
0

这是我根据上述帮助得出的结论:

@property (nonatomic, strong) IBOutlet UIToolbar *webToolbar;
@property (nonatomic, strong) UIBarButtonItem *backButton;
@property (nonatomic, strong) UIBarButtonItem *forwardButton;
@property (nonatomic, strong) UIBarButtonItem *refreshButton;
@property (nonatomic, strong) UIBarButtonItem *stopButton;
@property (nonatomic, strong) UIBarButtonItem *flexibleItem;

- (void)toolbarButtons
{
    self.backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"UIButtonBarArrowLeft.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(goBack)];

    self.forwardButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"UIButtonBarArrowRight.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(goForward)];

    self.backButton.enabled = [self.webView canGoBack];
    self.forwardButton.enabled = [self.webView canGoForward];

    self.flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                  target:nil
                                                                  action:nil];


    self.refreshButton = [[UIBarButtonItem alloc]
                      initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                      target:self action:@selector(refreshWebView)];

    self.refreshButton.style = UIBarButtonItemStyleBordered;

    self.stopButton = [[UIBarButtonItem alloc]
                      initWithBarButtonSystemItem:UIBarButtonSystemItemStop
                      target:self action:@selector(stop)];

    self.stopButton.style = UIBarButtonItemStyleBordered;

    if(self.webView.loading)
    {
        NSArray *items = [NSArray arrayWithObjects: self.backButton, self.forwardButton, self.flexibleItem, self.stopButton, nil];
        [self.webToolbar setItems:items animated:NO];
    }
    else
    {
        NSArray *items = [NSArray arrayWithObjects: self.backButton, self.forwardButton, self.flexibleItem, self.refreshButton, nil];
        [self.webToolbar setItems:items animated:NO];
    }
}
于 2012-12-01T22:48:44.187 回答