【问题标题】:Remove Cancel Button in UIImagePickerControler--iOS 5.0移除 UIImagePickerControler--iOS 5.0 中的取消按钮
【发布时间】:2011-12-25 02:15:39
【问题描述】:

我有一个应用程序,它在启动时显示 UIImagePickerController。控制器显示取消按钮,但没有可取消的内容。有什么办法可以去掉这个按钮?

注意:这不是Can the Cancel button be removed from a UIImagePickerController in OS 3.2? 的副本。 iOS 3.2 的答案在 iOS 5 中对我不起作用。

【问题讨论】:

  • 我在两个应用程序 4 和 5 中使用了它。但我没有看到任何取消按钮。你有什么不寻常的方式来呈现它吗?
  • 我在 viewDidAppear 中显示 UIImagePickerControler,这意味着一旦对话框被关闭,通过取消按钮,它就会重新出现。我提供的大部分代码都是从 Apple 文档中复制的,所以我不认为就是这样。我在模拟器上,如果有任何改变(虽然我没有使用相机类型——我使用的是保存的图片库)。
  • 如果视图打算作为根视图,为什么要使用另一个视图控制器来呈现它?
  • 因为处理完后我必须切换到另一个视图。我知道那会更容易,但我需要知道如何摆脱取消按钮。用户可以点击取消而不发生任何事情的事实在任何平台上都是愚蠢的,更不用说 iOS了。

标签: ios cocoa-touch uiimagepickercontroller


【解决方案1】:

我尝试不以模态方式放置 UIImagePickerController,而是以原始方式(就像我们使用普通自定义 UIViewController 一样)。所以,我不需要 UINavigationBar 中的取消按钮。

没有记录删除取消按钮的方法。但是here 我找到了一些想法。 我刚刚在我的 AppDelegate.m 文件中添加了这段代码:

- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    for (UINavigationItem *item in navigationController.navigationBar.subviews) {
        if ([[item title] compare:@"Cancel"] == 0) {
            UIButton *button = (UIButton *)item;
            [button setHidden:YES];
        }
    }
}

所以取消按钮现在被隐藏了。但是,如果您在相册中选择某个文件夹,则下一个视图将使用该按钮(并且它没有隐藏)。 然后我尝试添加这样的代码:

- (void)navigationController:(UINavigationController *)navigationController
didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    for (UINavigationItem *item in navigationController.navigationBar.subviews) {
        if ([[item title] compare:@"Cancel"] == 0) {
            UIButton *button = (UIButton *)item;
            [button setHidden:YES];
        }
    }
}

我得到取消按钮隐藏在 UIImagePickerController 导航内的所有导航堆栈中。

但当新视图出现时(动画转换),取消按钮也出现了。

我不知道如何解决这个问题。


但我认为这是一种棘手且低效的方法。因为它可能会在未来的 iOS 更新中破坏您的应用程序。所以你可以使用that问题的答案。但这是完全不同的方法。

附:对不起我的语言。

【讨论】:

  • 谢谢!这看起来正是我所需要的。试过后我会回复的。
  • 顺便说一下,@Yuriy Muzyukin,我不想用动画呈现图像选择器。我需要你一开始说的。感谢您的长篇回答!
【解决方案2】:

对于 iOS 7 和 8,上述解决方案将不起作用,

由于我们无法使用导航栏中的导航项,因此应该进行一些小的更改。以下将像魅力一样工作

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    viewController.navigationItem.rightBarButtonItems = nil;
}

- (void)navigationController:(UINavigationController *)navigationController
   didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    viewController.navigationItem.rightBarButtonItems = nil;
}

【讨论】:

    猜你喜欢
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多