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

问题描述 投票:26回答:6

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

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

它似乎有一些遮挡红色Color()的视图:

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

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

swift uiimagepickercontroller
6个回答
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

这导致:

enter image description here


20
投票

尝试:

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

代替

picker.navigationBar.backgroundColor = UIColor.redColor()

如果您想要半透明效果,请将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]};

8
投票

Swift = IOS 8 || 9

只是把这个方法

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

}

2
投票

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

picker.navigationBar.translucent = false

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

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


0
投票

UIImagePickerController是一个UINavigationController。它的风格可以与UINavigationController的风格相同。

© www.soinside.com 2019 - 2024. All rights reserved.