你如何着色/自定义UIImagePickerController的导航栏?

Fre*_*Lee 26 uiimagepickercontroller swift

为UIImagePickerController的导航栏着色的正确方法是什么?
我只是试图看到背景颜色,但我得到一种褪色的颜色,如下图所示; 好像有些观点阻碍了它.

let picker = UIImagePickerController()
picker.sourceType = type
picker.mediaTypes = [kUTTypeImage]
picker.delegate = self
picker.navigationBar.backgroundColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

它似乎有一些视图模糊redColor():

(lldb) po picker.navigationBar.subviews
2 values
 {
  [0] = 0x00007fe7bb52a890
  [1] = 0x00007fe7bb52b670
}
Run Code Online (Sandbox Code Playgroud)

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

Mic*_*hal 54

针对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
Run Code Online (Sandbox Code Playgroud)

这导致:

在此输入图像描述

  • 这在 iOS 13 中似乎不再适用——标题、背景和按钮颜色都是系统颜色。在 iOS 13 中,我可以更改图像选择器导航栏颜色的唯一方法是通过 UINavigationBar.appearance().tintColor = UIColor.white 等。但是,这不是一个理想的解决方案,因为它会更改所有导航栏的颜色。 (17认同)

rin*_*aro 20

尝试:

picker.navigationBar.translucent = false
picker.navigationBar.barTintColor = .redColor()
Run Code Online (Sandbox Code Playgroud)

代替

picker.navigationBar.backgroundColor = UIColor.redColor()
Run Code Online (Sandbox Code Playgroud)

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


小智 10

这是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]};
Run Code Online (Sandbox Code Playgroud)


Mit*_*iya 8

Swift = IOS 8 || 9

只是把这个方法

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

}
Run Code Online (Sandbox Code Playgroud)


小智 6

斯威夫特 5 / IOS 13

我找到了这个解决方案:

let barApperance = UINavigationBar.appearance()
barApperance.tintColor = .systemBlue
Run Code Online (Sandbox Code Playgroud)

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