Richard Burton
Richard Burton

Reputation: 2240

How to programmatically add a button to remove a WebView?

I have tried to add a UIBarButtonItem to my UINavigation bar but I can't get it to show up:

  - (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;
  }

I can't figure out why the button isn't there. Any help would be much appreciated!

Thanks.

Upvotes: 0

Views: 1012

Answers (3)

Prem Prakash
Prem Prakash

Reputation: 146

Richard, I understand what you want to achieve but I am not sure whether you are using a navigation controller already.

Case 1: using UINavigationController

If you are using Navigation Controller already then you don't need to add new Navigation bar.

If you are using NavigationController then show the use the bar from Navigation Controller and your code will work.

below two lines would not be required.

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

Case 2: Not using UINavigationController

If you are not using UINavigationController then you are required to add a Bar and add Bar button in the navigation item of 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]];

and you need to remove following line

[self.navigationItem setLeftBarButtonItem:backButton];

It will work perfectly. Let me know if you find any issues.

Upvotes: 1

user1525369
user1525369

Reputation:

Before add UIBarButtonItem to UINavigationBar, First change your UIWebView frame,

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

Here add (-50) to height of UIWebView.

And now, i just write code for how to add UIBarButtonItem to UINavigationBar:

   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];

Here is Method name is buttonActionthat execute/call when you tapped on UIBarButtonItem.

-(void)buttonAction
{
  //stuff of code;
}

Upvotes: 2

Balu
Balu

Reputation: 8460

remove these two lines from your code and check it,

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

or you can add like this,

    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]];

Upvotes: 1

Related Questions