这让我很烦恼!我只是想以编程方式向导航栏添加一个右侧“flash”按钮,但出于某种原因,我无法使其显示。我的代码如下。谁能帮我?提前致谢。
//Note: flashButton is declared as a UIButton property in .h, and synthesized in .m
UIView *flashContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
[flashButton addTarget:self action:@selector(toggleTorch) forControlEvents:UIControlEventTouchUpInside];
[flashButton setBackgroundImage: [UIImage imageNamed:@"FlashIconInactive_small.png"] forState:UIControlStateNormal];
[flashButton setBackgroundImage: [UIImage imageNamed:@"FlashIconActive_small.png"] forState:UIControlStateHighlighted];
[flashContainer addSubview:flashButton];
[flashButton sizeToFit];
UIBarButtonItem *flashButtonItem = [[UIBarButtonItem alloc] initWithCustomView:flashContainer];
scannerVC.delegate = self;
scannerVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTapped)];
[[scannerVC navigationItem] setRightBarButtonItem:flashButtonItem];
最佳答案
flashButton 是否通过 IBOutlet 连接到 xib 或 Storyboard 中的按钮?如果没有,您需要使用 +[UIButton buttonWithType:] 创建 UIButton 的实例。 .
另一个要尝试的测试是在第一行(addTarget: one)上设置一个断点,然后在调试器中输入 po flashButton
。输出是什么?如果它为零,则您的按钮不存在,需要创建。
关于ios - RightBarButtonItem 未出现在导航栏中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14542484/