iOS中如何在导航栏右边添加按钮,代码是这样的 UIButton *releaseButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [releaseButton setTitle:@"发布" forState:normal]; [releaseButton addTarget:self action:@selector(releaseInfo:) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *releaseButtonItem = [[UIBarButtonItem alloc] initWithCustomView:releaseButton]; self.navigationItem.rightBarButtonItem = releaseButtonItem; |
|
#230分 |
代码看着没什么问题,你可以DEBUG一下在添加时self.navigationController是不是nil
|
#325分 |
确保你当前的控制器在导航控制器栈中(UINavigationController),意思是当前的控制器或为uinavigationcontroller 的rootViewController或为uinavigationcontroller 的viewcontrollers 中的一员
|
#425分 |
如果是这种情况你可以尝试用uinavigationcontroller 包装一下
UIViewController *yourcontroller = ……. UINavigationController *navController =[[UINavigationController alloc] initWithRootViewController: yourcontroller ] |
#5 |
回复2楼: self.navigationController吗? 试了一下,不是nil 可右边按钮为什么显示不出来呢? |
#6 |
回复3楼: 该控制器是在在故事板中做的(拖了一个UINavigationController),主控制器中用如下代码来加载故事版 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Help" bundle:nil]; UINavigationController *nav = [storyboard instantiateViewControllerWithIdentifier:@"help"]; HelpViewController *help = (HelpViewController *)nav.topViewController; help.navTitle = @"帮办"; [self presentViewController:nav animated:true completion:nil]; 关键是该控制器添加的左边的返回按钮能显示出来,右边的按钮却没添加上 |
#8 |
初始化frame
releaseButton.frame = CGRectMake(0, 0, 80.0f, 30.0f); |
#10 |
[
UIBarButtonItem *releaseButon=[[UIBarButtonItem alloc] initWithTitle:@”发布” style:UIBarButtonItemStylePlain target:self action:@selector(releaseInfo:)]; self.navigationItem.rightBarButtonItem=releaseButon; ] 这样就行了 |