iphone - 如何以编程方式添加按钮以删除 WebView?

标签 iphone ios objective-c

我试图将 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;
  }

我不明白为什么没有按钮。任何帮助将不胜感激!

谢谢。

最佳答案

在将 UIBarButtonItem 添加到 UINavigationBar 之前,首先更改您的 UIWebView 框架,

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

此处将 (-50) 添加到 UIWebView 的高度。

现在,我只是编写如何将 UIBarButtonItem 添加到 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];

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

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

关于iphone - 如何以编程方式添加按钮以删除 WebView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17668949/

相关文章:

iphone - 如何在 Iphone 上传输我编码和编译的 "hello world"iphone 应用程序?

ios - subview 重叠的问题

ios - 在应用程序准备好销售后编辑 5.5 屏幕、4.0 屏幕、3.5 屏幕和 iPad 屏幕的屏幕截图

objective-c - 如何以编程方式在 MAC OS X 中查找 MAC 地址?

iphone - 如何在符号化崩溃报告(iPhone/Mac)中获取正确的行号?

iphone - 在 iOS 应用程序中创建持久任务队列的最佳实践是什么?

iphone - iOS 上的延迟执行技术

ios - SwiftUI - 根据条件添加导航栏按钮

objective-c - UIImage 和 NSCoding iOS 5.1

iphone - UITextView 对齐方式正在自行更改