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