0

我试图将 UIBarButtonItem 添加到我的 UINavigation 栏,但我无法让它显示:

  - (void)viewDidLoad {
      [super viewDidLoad];

      self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.view.frame.size.height)];
      [self.view addSubview:self.webView];

      UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                    initWithTitle:NSLocalizedString(@"Back", @"")
                                    style:UIBarButtonItemStyleDone
                                    target:self
                                     action:@selector(remove:)];

      [self.navigationItem setLeftBarButtonItem: backButton];

       UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];


      [self.view addSubview:myBar];

      self.webView.delegate = self;

      NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
      [self.webView loadRequest:request];

  }

  - (void)remove:(id)sender
  {
      [self removeFromParentViewController];
      [self.webView stopLoading];
      self.webView.delegate = nil;
  }

我无法弄清楚为什么按钮不存在。任何帮助将非常感激!

谢谢。

4

3 回答 3

2

在添加之前UIBarButtonItemUINavigationBar首先改变你的UIWebView框架,

self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.view.frame.size.height - 50 )];

这里将(-50)添加到 的高度UIWebView

现在,我只是编写代码来说明如何添加UIBarButtonItemUINavigationBar

   UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    [self.view addSubview:navBar];
    UIBarButtonItem *Button = [[UIBarButtonItem alloc]
                               initWithTitle:@"MyButton"
                               style:UIBarButtonItemStyleBordered
                               target:self
                               action:@selector(buttonAction)];

    UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"This is My Title "];
    item.rightBarButtonItem = Button;
    item.hidesBackButton = YES;
    [navBar pushNavigationItem:item animated:NO];

这是方法名称buttonAction,当您点击时执行/调用UIBarButtonItem

-(void)buttonAction
{
  //stuff of code;
}
于 2013-07-16T06:07:47.080 回答
1

从您的代码中删除这两行并检查它,

 UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
      [self.view addSubview:myBar];

或者你可以像这样添加,

    UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];    
    [self.view addSubview:myBar];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"remove"
                                   style:UIBarButtonItemStyleDone
                                   target:self
                                   action:@selector(remove:)];
    UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@"remove"];
    [item setLeftBarButtonItem:backButton];
    [myBar setItems:[NSArray arrayWithObject:item]];
于 2013-07-16T05:53:41.080 回答
1

理查德,我了解您想要实现的目标,但我不确定您是否已经在使用导航控制器。

案例一:使用 UINavigationController

如果您已经在使用导航控制器,那么您不需要添加新的导航栏。

如果您使用的是 NavigationController,则显示使用来自 Navigation Controller 的栏,您的代码将起作用。

不需要低于两行。

UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; [self.view addSubview:myBar];

案例 2:不使用 UINavigationController

如果你没有使用 UINavigationController 那么你需要添加一个 Bar 并在 Bar 的导航项中添加 Bar 按钮。

UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; [self.view addSubview:myBar];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                               initWithTitle:@"remove"
                               style:UIBarButtonItemStyleDone
                               target:self
                               action:@selector(remove:)];
UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:@"remove"];
[item setLeftBarButtonItem:backButton];
[myBar setItems:[NSArray arrayWithObject:item]];

并且您需要删除以下行

[self.navigationItem setLeftBarButtonItem:backButton];

它将完美地工作。如果您发现任何问题,请告诉我。

于 2013-07-16T07:08:31.497 回答