问题描述
我正在创建一个 UIActivityViewController 并尝试在点击共享消息图标后更改其文本颜色.
I am creating a UIActivityViewController and trying to change its text color after tapping the share messages icon.
默认情况下,我在 AppDelegate
中将导航栏文本颜色设置为白色
By default, I set my navigation bar text colors to white in the AppDelegate
like so
UINavigationBar.appearance().tintColor = whiteColor
UIBarButtonItem.appearance().tintColor = whiteColor
但是,对于 UIActivityViewController,我想将其设为默认值(即黑色标题文本和蓝色 Cancel
按钮)
However for just the UIActivityViewController I want to make it the default (i.e. black title text, and the blue Cancel
button)
我尝试了以下方法但没有成功:
I have tried the following to no luck:
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)
白色文本结果还是一样:
The result is still the same with white text:
如果您仔细观察图片,导航栏的标题和右侧栏按钮项中都有白色文本.
If you look closely in the image, the navigation bar has white text in the title and right bar button items.
推荐答案
对于尝试做类似事情的其他人.UIActivityViewController 在完成时给我们一个回调.我们可以做的是,更改 UIBarButtonItem 颜色,然后在完成后将其设置回我们原来的颜色.
For someone else trying to do something similar to this. UIActivityViewController gives us a callback on its completion. What we can do is, change UIBarButtonItem color and then, on its completion, set it back to our original color.
对我来说,完成处理程序没有正确地将颜色设置回原始颜色,所以我必须在 viewWillAppear 中进行设置.
For me, the completion handler is not correctly setting the color back to original, so i am gonna have to do it in the 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)
}
这篇关于UIActivityViewController 更改导航栏文字颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!