改变SLComposeServiceViewController按钮标题? [英] Change the button titles on SLComposeServiceViewController?

查看:567
本文介绍了改变SLComposeServiceViewController按钮标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法改变对SLComposeServiceViewController按钮标题?我试图改变在导航项目的栏按钮的项目,但这些都不是正确的按钮。

Is there a way to change the button titles on the SLComposeServiceViewController? I tried to change the bar button items on the navigation item, but those aren't the right buttons.

推荐答案

编辑#3:解决方案的工作在iOS 9和iOS 10测试版

在previous办法停止与iOS 9个工作,但以下似乎再工作(在iOS 9日和10 Beta 2的测试):

The previous approach stopped working with iOS 9, but the following seems to work again (tested on iOS 9 and 10 beta 2):

1)首先,你需要添加一个UIFont类扩展检查一个按钮字体为粗体(这一点,因为邮政按钮始终黑体);这里是如何

1) First, you need to add a UIFont class extension to check if a button font is bold (this, because the Post button is always bold); here's how.

2)然后,在 viewDidAppear:,我们需要以下code(在code我编辑2)写的更新版本:

2) Then, in viewDidAppear:, we need the following code (an updated version of the code I wrote in 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)
                }
            }
        }
    }
}

当然,这个工作现在,但它可能会在未来再度爆发......

Of course, this works now, but it will probably break again in the future...

编辑#2:我做了一个设备上运行的iOS 8.4)

原来我错了,愿意花时间不合理量后的我已经能够同时改变按钮的颜色和他们的文字

Turns out I was wrong, after spending an unreasonable amount of time on this I've been able to both change the color of the buttons and their text.

下面是我的code,即需要放在里面的 ViedDidAppear() (如果你把它放在 viewDidLoad中()它不会工作):

Here's my code, that needs to be put inside ViedDidAppear() (if you place it in viewDidLoad() it won't work!):

    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)。
另外,应注意的是的此可以随时破的,即使它不应当如易碎的previous溶液(它通过子视图并试图遍历向下转换它们,所以小在视图层次结构的变化不应该使其无效);我的期望是,即使在未来它停止工作,它应该不会崩溃共享扩展。

Still, I'm not sure this should be done on a shipping app nor it would pass App Review (it should, though, because it doesn't mess with private APIs). Also, it should be noted that this could break anytime, even though it shouldn't be as easily breakable as the previous solutions (it iterates through the subviews and attempts downcasting them, so a small change in the view hierarchy shouldn't render it useless); my expectations is that, even if in the future it stops working, it shouldn't crash the Share Extension.

原来的答复

我相信你(我)想要做什么是不可能的了,可能是由设计。这里的原因:

I believe what you (and I) want to do is not possible anymore, possibly by design. Here's why:

由@Kasztan和@Paito答案的启发,我在尝试这个 viewDidLoad中()我ShareViewController的:

Inspired by @Kasztan and @Paito answers, I tried this in viewDidLoad() of my ShareViewController:

    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()
                    }
                }
            }
        }
    }

不,我相信这样的事情应该在一个应用程序的船,但作为一个概念证明这应该有工作,在理论上,应该是与操作系统的未来版本少了几分破碎

Not that I believe something like this should ship in an app, but as a proof of concept this should have worked and, in theory, should be a bit less breakable with future releases of the OS.

除,但它并没有为我工作在iOS 8.4:我看到所有的检查点信息记录上,从1到5,其中一些多次(因为它应该是,因为$ C $ç尝试每一种可能的子视图)。

Except, it didn't work for me on iOS 8.4: I see all the checkpoint messages logged, from 1 to 5, some of them multiple times (as it should be, since the code tries every possible subview).

5消息记录了两次,这是有道理的,因为这意味着它成功地低垂着这两个按钮,取消和职位,但没有文字,也没有颜色更改默认的。

The "5" message is logged twice, which makes sense since it means that it successfully downcast both the buttons, Cancel and Post, but not the text nor the color is changed from the default.

我的结论是,在事情的苹果code prevents我们改变这些按钮的外观

My conclusion is that something in Apple's code prevents us to change the appearance of those buttons.

当然,如果有人发现了一个解决方案,我会很高兴downvote我自己的答案(如果这是可以做到的,我注意到肯定);)

Of course, if anyone finds a solution, I'd be glad to downvote my own answer (if it can be done, I'm note sure) ;)

编辑#1 :最后一个检查,我登录按钮标题太,经过按钮向下转换(5),是的,我得到了可选的(取消),并可选(邮报 )在控制台上,所以这种解决方案得到正确的按钮,但它们不能被编辑。

EDIT #1: One last check, I logged the button title too, after the buttons downcast (5), and yes, I got Optional("Cancel") and Optional("Post") in the console, so this solution gets the right buttons, but they can't be edited.

这篇关于改变SLComposeServiceViewController按钮标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆