我曾尝试将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;
}
我无法弄清楚按钮不存在的原因。任何帮助将不胜感激!
感谢。
答案 0 :(得分:2)
在将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;
}
答案 1 :(得分: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]];
答案 2 :(得分:1)
案例1:使用UINavigationController
如果您使用的是导航控制器,则无需添加新的导航栏。
如果您正在使用NavigationController,则显示使用导航控制器中的栏,您的代码将起作用。
下面两行不需要。
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];
它会完美运作。如果您发现任何问题,请告诉我。