Ios8 共享扩展名取消和发布按钮

Ios8 共享扩展名取消和发布按钮,ios8,ios8-share-extension,Ios8,Ios8 Share Extension,您能帮助我如何在SLComposeService ViewController中自定义取消和发布按钮吗 我想更改标题和按钮图像。SLComposeServiceViewController自定义其UI的选项非常有限,目前还不包括修改“取消”和“发布”按钮的功能。在当前版本的iOS中,避免使用这些按钮的唯一方法是不要使用SLComposeServiceViewController。使用该类不需要共享扩展,可以使用完全自定义的UI。如果这些按钮不合适,那是您唯一的选择。我刚刚找到了一种方法: cla

您能帮助我如何在SLComposeService ViewController中自定义取消和发布按钮吗


我想更改标题和按钮图像。

SLComposeServiceViewController
自定义其UI的选项非常有限,目前还不包括修改“取消”和“发布”按钮的功能。在当前版本的iOS中,避免使用这些按钮的唯一方法是不要使用
SLComposeServiceViewController
。使用该类不需要共享扩展,可以使用完全自定义的UI。如果这些按钮不合适,那是您唯一的选择。

我刚刚找到了一种方法:

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

请注意-这是一个脆弱的解决方案,基于
SLComposeServiceViewController
的未记录内部结构,我提出了此解决方案。最糟糕的情况是,苹果公司改变了一切,并停止使用导航栏,在这种情况下,按钮将简单地恢复为“Post”和“Cancel”。不应该发生车祸

在这个例子中,我使用了'✖' 取消并✔' 去邮局

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // find the navigation bar
    UINavigationBar* bar = self.navigationController.navigationBar;
    if (bar == nil)
    {
        for (UIView* b in self.view.subviews)
        {
            if ([b isKindOfClass:UINavigationBar.class])
            {
                bar = (UINavigationBar*)b;
                break;
            }
        }
        if (bar == nil)
        {
            return;
        }
    }

    // find the cancel and post buttons, assuming the post button is on the far right which is a common iOS UI design, to put the positive or confirm action on the right
    // also deals with right to left languages (which SLComposeViewController does not support yet), where the post button will be on the left
    UIButton* postButton = nil;
    UIButton* cancelButton = nil;
    BOOL rightToLeft = NO;
    if ([UIView.class respondsToSelector:@selector(userInterfaceLayoutDirectionForSemanticContentAttribute:)])
    {
        rightToLeft = ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:bar.semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft);
    }
    CGFloat x = (rightToLeft ? FLT_MAX : FLT_MIN);
    for (UIView* v in bar.subviews)
    {
        if ([v isKindOfClass:UIButton.class] && ((rightToLeft && v.frame.origin.x < x) || (!rightToLeft && v.frame.origin.x > x)))
        {
            x = v.frame.origin.x;
            if (postButton != nil)
            {
                cancelButton = postButton;
            }
            postButton = (UIButton*)v;
        }
    }

    // if we found a cancel UIButton, set the title
    if (cancelButton != nil)
    {
        [cancelButton setTitle:@"✖" forState:UIControlStateNormal];
    }

    // if we found a post UIButton, set the title
    if (postButton != nil)
    {
        [postButton setTitle:@"✔" forState:UIControlStateNormal];
    }
}
-(void)视图显示:(BOOL)动画
{
[超级视图显示:动画];
//找到导航栏
UINavigationBar*bar=self.navigationController.navigationBar;
如果(bar==nil)
{
for(self.view.subview中的UIView*b)
{
if([b是类的种类:UINavigationBar.class])
{
bar=(UINavigationBar*)b;
打破
}
}
如果(bar==nil)
{
返回;
}
}
//找到cancel(取消)和post(发布)按钮,假设post(发布)按钮位于最右侧,这是常见的iOS UI设计,将肯定或确认操作置于右侧
//还涉及从右到左的语言(SLComposeViewController尚不支持),其中post按钮将位于左侧
UIButton*postButton=nil;
UIButton*cancelButton=nil;
布尔右至左=否;
if([UIView.class respondsToSelector:@selector(userInterfaceLayoutDirectionForSemanticContentAttribute:))
{
rightToLeft=([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:bar.semanticContentAttribute]==UIUserInterfaceLayoutDirectionRightToLeft);
}
CGFloat x=(右至左?FLT_最大值:FLT_最小值);
用于(条形子视图中的UIView*v)
{
if([v iskindof类:UIButton.class]&&&((rightoleft&&v.frame.origin.xx)))
{
x=v.frame.origin.x;
如果(postButton!=无)
{
cancelButton=postButton;
}
postButton=(UIButton*)v;
}
}
//如果我们发现一个取消按钮,设置标题
如果(取消按钮!=无)
{
[取消按钮设置标题:@”✖" forState:uicontrol状态正常];
}
//如果我们找到了post按钮,请设置标题
如果(postButton!=无)
{
[postButton设置标题:@”✔“forState:uicontrol状态正常];
}
}