如何以编程方式添加按钮以删除WebView?
我曾尝试到的UIBarButtonItem添加到我的UINavigation酒吧,但我不能让它显示出来:如何以编程方式添加按钮以删除WebView?
- (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;
}
我想不通为什么按钮是不存在的。任何帮助将非常感激!
谢谢。
删除您这两行代码,并检查它,
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]];
前添加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:使用UINavigationController的
如果您正在使用导航控制器已经那么你并不需要增加新的导航栏。
如果您使用NavigationController,然后显示使用导航控制器的栏,您的代码将工作。
下面两行不需要。 UINavigationBar * myBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,320,50)]; [self.view addSubview:myBar];
案例2:如果您不使用UINavigationController的,那么你需要添加一个酒吧和酒吧的导航项目添加栏按钮,不使用的UINavigationController
。 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];
这将很好地工作。如果您发现任何问题,请告知我。
感谢您花时间回答。我正在使用UINavigationController。 –
@Richard Burton:一旦检查我的更新答案。 – Balu
谢谢阳光,真的很有帮助。 –