UIActivityViewController更改导航栏文本颜色

8

我将创建一个UIActivityViewController并尝试在点击分享消息图标后更改其文本颜色。

默认情况下,我在AppDelegate中将导航栏文本颜色设置为白色,如下所示:

UINavigationBar.appearance().tintColor = whiteColor
UIBarButtonItem.appearance().tintColor = whiteColor

然而,对于仅限UIActivityViewController,我希望将其设置为默认选项(即黑色标题文本和蓝色的取消按钮)

我尝试了以下方法,但都没有成功:

let shareText = "text to share"
let activityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: [])

activityViewController.navigationController?.navigationBar.tintColor = UIColor.black
activityViewController.navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.blue

present(activityViewController, animated: true, completion: nil)

白色文本的结果仍然相同:

如果你仔细查看图片,导航栏在标题和右侧按钮项中有白色文本。

enter image description here


1
在呈现时使用 UINavigationBar.appearance().tintColor = black,而在取消时使用 UINavigationBar.appearance().tintColor = whiteColor。 - KKRocks
我该如何确定它是否存在? - Simon
你需要确定什么颜色? - KKRocks
它是否在iOS 11上可用?@KKRocks的评论在iOS 11中不起作用。 - wzhang84
@Simon,你找到这个问题的解决方案了吗? - Sunil Targe
5个回答

9

如果有其他人想要做类似的事情,可以使用UIActivityViewController来获得它完成时的回调。我们所能做的是改变UIBarButtonItem颜色,然后在完成时将其设置回原始颜色。

对于我而言,完成处理程序没有正确地将颜色设置回来,所以我需要在viewWillAppear中进行操作。

// Setting the required color for the bar buttons on share activities
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.yourRequiredColor], for: .normal)

// Re-setting the default color for the bar buttons again on activity's completion
activityViewController.completionWithItemsHandler = { (activityType, completed:Bool, returnedItems:[Any]?, error: Error?) in
    UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.yourOriginalColor], for: .normal)
}

我想补充一点,您还应该设置UIButton.appearance() - 某些共享操作(例如保存到文件夹按钮)不受UIBarButtonItem的影响。 - Aron

4
在iOS 11中,您可以通过设置以下内容来更改“通过消息屏幕共享”中的取消按钮颜色:
UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .blue

1
我已尝试了以上所有代码,但在取消并再次打开操作表时出现错误。因此,我想出了以下解决方案,希望对其他人也有用。
UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .systemBlue
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .systemBlue
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.systemBlue], for: .normal)
activityVC.completionWithItemsHandler = { (activityType, completed: Bool, _: [Any]?, _: Error?) in
    if activityType == nil || completed {
        UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
        UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .normal)
    }
}

0
如果您在应用程序委托类中自定义导航栏,则应该在分享操作之前(当按下分享按钮时等)更改它,但不要忘记在完成或关闭共享操作时将其更改回来,因此您需要在具有共享操作的类的viewWillAppear函数中自定义导航栏。
应该是这样的:
class ShareActionViewController: UIViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        //this should be same with your appDel settings:
        UINavigationBar.appearance().tintColor = .white
        UINavigationBar.appearance().barTintColor = .appPurpleColor
    }

    fileprivate func shareButtonPressed() {
        UINavigationBar.appearance().tintColor = .purple
        UINavigationBar.appearance().barTintColor = .white 
    }

}

-1

我正在使用iOS 12,并且只有在我做了这个操作后才能正常工作:

static func setSystemNavigationBar() {
    UINavigationBar.appearance().tintColor = .black
    UINavigationBar.appearance().titleTextAttributes =
        [NSAttributedString.Key.foregroundColor : UIColor.black,
         NSAttributedString.Key.font: UIFont(name: "Aller", size: 20)!]
    UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.blue], for: .normal)
}

您可以根据自己的喜好更改字体和颜色。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接