【问题标题】:How do you color/customize the UIImagePickerController's Navigation Bar?你如何为 UIImagePickerController 的导航栏着色/自定义?
【发布时间】:2014-11-27 06:18:46
【问题描述】:

为 UIImagePickerController 的导航栏着色的正确方法是什么?
我只是试图查看背景颜色,但我得到了一种褪色的颜色,如下图所示;好像有什么视线挡住了它。

let picker = UIImagePickerController()
picker.sourceType = type
picker.mediaTypes = [kUTTypeImage]
picker.delegate = self
picker.navigationBar.backgroundColor = UIColor.redColor()

它似乎有一些遮挡 redColor() 的视图:

(lldb) po picker.navigationBar.subviews
2 values
 {
  [0] = 0x00007fe7bb52a890
  [1] = 0x00007fe7bb52b670
}

为导航栏创建纯色的正确方法是什么?

【问题讨论】:

    标签: swift uiimagepickercontroller


    【解决方案1】:

    为 Swift 4.2 更新

    为了完整起见,我将添加全色自定义设置:

    let imagePicker = UIImagePickerController()
    imagePicker.navigationBar.isTranslucent = false
    imagePicker.navigationBar.barTintColor = .blue // Background color
    imagePicker.navigationBar.tintColor = .white // Cancel button ~ any UITabBarButton items
    imagePicker.navigationBar.titleTextAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor.white
    ] // Title color
    

    导致:

    【讨论】:

    • 我试过你的代码它工作正常但取消按钮没有显示
    • 这似乎不再适用于 iOS 13 —— 标题、背景和按钮颜色都是系统颜色。在 iOS 13 中,我可以更改图像选择器导航栏颜色的唯一方法是通过 UINavigationBar.appearance().tintColor = UIColor.white 等。但是,这不是一个理想的解决方案,因为它会更改所有导航栏的颜色。
    • tomblah 的评论仍然适用于带有 PHPickerViewController 的 iOS 14。我只是确保在关闭选择器后将色调颜色设置回之前的值。 let startingColor = UINavigationBar.appearance().tintColor; UINavigationBar.appearance().tintColor = .white 然后关闭 UINavigationBar.appearance().tintColor = startingColor
    【解决方案2】:

    试试:

    picker.navigationBar.translucent = false
    picker.navigationBar.barTintColor = .redColor()
    

    而不是

    picker.navigationBar.backgroundColor = UIColor.redColor()
    

    如果您想要半透明效果,请将translucent = true 保留为默认值。

    【讨论】:

    • 是的。这就是我要找的。谢谢。
    【解决方案3】:

    这里是 Objective-C 中正确的解决方案代码。可能有用。

    imagePickerController.navigationBar.translucent = NO;
    imagePickerController.navigationBar.barTintColor = [UIColor colorWithRed:0.147 green:0.413 blue:0.737 alpha:1];
    imagePickerController.navigationBar.tintColor = [UIColor whiteColor];
    imagePickerController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    

    【讨论】:

      【解决方案4】:

      Swift = IOS 8 || 9

      就放这个方法

      func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) 
      {
           imagePicker.navigationBar.tintColor = .whiteColor()
           imagePicker.navigationBar.titleTextAttributes = [
           NSForegroundColorAttributeName : UIColor.whiteColor()
           ]
      
      }
      

      【讨论】:

        【解决方案5】:

        Swift 5 / IOS 13

        我找到了这个解决方案:

        let barApperance = UINavigationBar.appearance()
        barApperance.tintColor = .systemBlue
        

        把它放在你创建 UIImagePickerController() 的地方

        【讨论】:

        • 感谢 SwiftUI 和自定义导航对我有用。否则,在带有白色标题的浅色模式下,添加和取消按钮根本不可见。
        • 这会对其他地方有影响吗?如果我们在不同的视图控制器中使用自定义样式来导航栏
        【解决方案6】:

        对于 Swift,IOS 8-10 正如 rintaro 提到的,我认为这里的主要问题是更改选择器导航栏的默认半透明属性:

        picker.navigationBar.translucent = false

        如果您在应用程序的某处设置此选项,这将导致导航栏使用 UINavigationBar 外观。

        如果您需要其他颜色,可以使用
        picker.navigationBar.barTintColor = UIColor.someColor

        【讨论】:

          【解决方案7】:

          UIImagePickerControllerUINavigationController。有可能 样式与UINavigationController 的样式相同。

          【讨论】:

            【解决方案8】:
            [[UINavigationBar appearance] setTintColor:[UIColor blueColor]];
            [[UINavigationBar appearance] setTitleTextAttributes:
                [NSDictionary dictionaryWithObjectsAndKeys:
                 [UIColor whiteColor], nil]];
            

            这适用于我在 iOS 14 上。没有其他工作。

            【讨论】:

              猜你喜欢
              • 2012-09-17
              • 1970-01-01
              • 2013-12-21
              • 1970-01-01
              • 2014-08-05
              • 2011-07-31
              • 2018-04-17
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多