更改SLComposeServiceViewController上的button标题?

有没有办法改变SLComposeServiceViewController的button标题? 我试图更改导航条上的栏button项目,但这些不是正确的button。

从navigationController!.navigationBar简单地访问魅力。 以下应该有所帮助。

self.navigationController!.navigationBar.topItem!.rightBarButtonItem!.title = "Save" 

我只是find了一个方法来做到这一点:

 class CustomServiceViewController: SLComposeServiceViewController { override func viewDidLoad() { let navigationBar = view.subviews.first?.subviews?.last? as? UINavigationBar let postButton = navigationBar?.subviews.last? as? UIButton let cancelButton = navigationBar?.subviews.last? as? UIButton postButton?.setTitle("Done", forState: .Normal) } } 

被警告 – 这是一个脆弱的解决scheme,基于SLComposeServiceViewController无证内部

Kasztan的回答不再适用于最新的iOS; 这里是最新的脆弱的解决scheme..

class CustomServiceViewController: SLComposeServiceViewController { override func viewDidLoad() { let navigationBar = view.subviews.last?.subviews?.last? as? UINavigationBar let postButton = navigationBar?.subviews[3] as? UIButton postButton?.setTitle("Done", forState: .Normal) } }

编辑#3:在iOS 9和iOS 10 beta上运行的解决scheme

以前的方法停止使用iOS 9,但以下似乎再次工作(在iOS 9和10testing版2上testing):

1)首先,你需要添加一个UIFont类扩展来检查button字体是否为粗体(这是因为Postbutton总是以粗体显示); 这里是如何 。

2)然后,在viewDidAppear: ,我们需要下面的代码(我在Edit 2中编写的代码的更新版本):

 if let navigationBar = self.navigationController?.navigationBar { // First, let's set backgroundColor and tintColor for our share extension bar buttons navigationBar.backgroundColor = UIColor.darkGrayColor() navigationBar.tintColor = UIColor.whiteColor() if let navBarSubviews = navigationBar.subviews as? [UIView] { for eachView in navBarSubviews { if let navBarButton = eachView as? UIButton { // Second, let's set our custom titles for both buttons (Cancel and Post); checking for the title wouldn't work for localized devices, so we check if the button is bold (Post) or not (Cancel) via the UIFont class extension above. let buttonFont : UIFont? = navBarButton.titleLabel?.font if buttonFont?.isBold == true { navBarButton.setTitle("Save", forState: .Normal) } else { navBarButton.setTitle("Cancel", forState: .Normal) } } } } } 

当然,这个现在可行,但未来可能会再次破裂。


编辑#2:我使它在iOS 8.4的设备上工作:)

事实certificate,我错了,花了不必要的时间在这个我已经能够改变button的颜色和他们的文字

这是我的代码, 需要放在ViedDidAppear() (如果你把它放在viewDidLoad()它不会工作!):

  if let navigationBar = self.navigationController?.navigationBar { // First, let's set backgroundColor and tintColor for our share extension bar buttons navigationBar.backgroundColor = UIColor.darkGrayColor() navigationBar.tintColor = UIColor.whiteColor() if let navBarSubviews = navigationBar.subviews as? [UIView] { for eachView in navBarSubviews { if let navBarButton = eachView as? UIButton { // Second, let's set our custom titles for both buttons (Cancel and Post); checking for the title wouldn't work on localized devices, so we check if the current button is emphasized (Post) or not (Cancel) via an UIFontDescriptor. let fontDescriptor : UIFontDescriptor? = navBarButton.titleLabel?.font.fontDescriptor() if let descriptor = fontDescriptor { let fontAttributes : NSDictionary = descriptor.fontAttributes() var buttonFontIsEmphasized : Bool? = fontAttributes["NSCTFontUIUsageAttribute"]?.isEqualToString("CTFontEmphasizedUsage") if buttonFontIsEmphasized == true { navBarButton.setTitle("Save", forState: .Normal) } else { navBarButton.setTitle("Cancel", forState: .Normal) } } } } } } 

不过, 我不确定这应该在运输应用程序上完成,也不会通过应用程序审查 (它应该,因为它不会混淆私人API)。 此外,应该指出的是, 这可能会随时打破 ,即使它不应该像以前的解决scheme那样容易打破(它遍历子视图并尝试向下转换它们,所以视图层次结构中的小改变不应该使其变得无用); 我的期望是,即使将来它停止工作,也不应该使共享分机崩溃。


原始答案

我相信你(和我)想要做的事情已经不可能了,可能是通过devise。 原因如下:

受@Kasztan和@Paito的启发,我在ShareViewController的viewDidLoad()中试了这个:

  for eachView in view.subviews { println("1") for eachSubView in eachView.subviews { println("2") if let navigationBarView = eachSubView as? UINavigationBar { println("3") for eachNavBarSubView in navigationBarView.subviews { println("4") if let navBarButton = eachNavBarSubView as? UIButton { println("5") println(navBarButton.titleForState(.Normal)) navBarButton.setTitleColor(UIColor.redColor(), forState: .Normal) navBarButton.setTitle("My text", forState: .Normal) navBarButton.tintColor = UIColor.redColor() navBarButton.setNeedsLayout() navBarButton.layoutIfNeeded() } } } } } 

不是说我相信像这样的东西应该在应用程序中发布 ,但作为一个概念的certificate,这应该起作用,理论上,应该是未来的操作系统版本less一些易碎。

除此之外,它不适用于我的iOS 8.4: 我看到所有的检查点消息logging ,从1到5,其中一些多次(因为它应该是,因为代码尝试每个可能的子视图)。

“5”消息被logging了两次,这是有意义的,因为这意味着它成功地向下同时按下Cancel和Postbutton,但不是文本,也不是默认的颜色。

我的结论是苹果代码中的某些东西阻止我们改变这些button的外观

当然,如果有人find解决办法的话,我会很乐意降低自己的答案(如果可以的话,我很确定);)

编辑#1 :最后一个检查,我logging了button标题之后,buttondowncast(5),是的,我有控制台中的可选(“取消”)和可选(“发布”),所以这个解决scheme得到右键,但不能编辑。