iOS 15 上的有限库选择器中的导航栏颜色不正确

Incorrect navigation bar colour in limited library picker on iOS 15

我正在尝试设置有限库选择器中导航栏的颜色。为此,我在 AppDelegate.application:didFinishLaunchingWithOptions:

中添加了以下几行
if (@available(iOS 13, *)) {
    UINavigationBarAppearance *navigationBarAppearance = [UINavigationBarAppearance new];
    navigationBarAppearance.backgroundColor = UIColor.yellowColor;

    [UINavigationBar appearance].standardAppearance = navigationBarAppearance;
    [UINavigationBar appearance].scrollEdgeAppearance = navigationBarAppearance;
}

[UINavigationBar appearance].barTintColor = UIColor.greenColor;
[UINavigationBar appearance].backgroundColor = UIColor.redColor;

这将颜色设置为绿色,正如我在 iOS 14 上所期望的那样:

但是 iOS 15 上的红色褪色了:

如何在 iOS 15 上正确设置颜色?

编辑: 最小可重现示例(使用单个视图控制器设置一个新的 iOS 项目并将 NSPhotoLibraryUsageDescription 键添加到 info.plist)

import UIKit
import Photos
import PhotosUI

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        PHPhotoLibrary.requestAuthorization(for: .readWrite, handler: { _ in})
        PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
    }
}

AppDelegate.swift:

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        if #available(iOS 13, *) {
            let navigationBarAppearance = UINavigationBarAppearance()
            navigationBarAppearance.backgroundColor = UIColor.yellow

            UINavigationBar.appearance().standardAppearance = navigationBarAppearance
            UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
        }

        UINavigationBar.appearance().barTintColor = UIColor.green
        UINavigationBar.appearance().backgroundColor = UIColor.red
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }
}

编辑: Objective-C 马特解决方案的版本:

UIGraphicsImageRenderer * imageRenderer = [[UIGraphicsImageRenderer alloc]initWithSize:CGSizeMake(1, 1)];
UIImage *image = [imageRenderer imageWithActions:^(UIGraphicsImageRendererContext *context){
    [TOOLBAR_BACKGROUND_COLOUR setFill];
    [context fillRect:CGRectMake(0, 0, 1, 1)];
}];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

完全控制导航栏颜色的方法是使用背景图像。制作图像渲染器。用纯红色填充上下文并从渲染器中提取图像。在外观代理上调用 setBackgroundImage

我没有时间将代码从 Swift 翻译成 Objective-C,所以我把它留给你了:

    let rend = UIGraphicsImageRenderer(size:.init(width:100, height:100))
    let im = rend.image {
        con in
        UIColor.red.setFill()
        con.fill(.init(x: 0, y: 0, width: 100, height: 100))
    }
    UINavigationBar.appearance().setBackgroundImage(im, for: .default)