调用函数时无法隐藏iOS工具栏

时间:2021-08-06 16:04:34

I'm building a meme generator and I'm attempting to hide the toolbar when an action is completed. I'm unsure as to why the toolbar is always visible.

我正在构建一个模因生成器,并且我正在尝试在操作完成时隐藏工具栏。我不确定为什么工具栏总是可见的。

func generateMemedImage() -> UIImage {
        navigationController?.setToolbarHidden(false, animated: false)

        // Render view to an image
        UIGraphicsBeginImageContext(self.view.frame.size)
        view.drawHierarchy(in: self.view.frame, afterScreenUpdates: true)
        let memedImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        navigationController?.setToolbarHidden(true, animated: false)
        return memedImage
    }

That function is called by the following function:

该函数由以下函数调用:

func save() {
        let memedImage = generateMemedImage()
        let meme = Meme(topText: topTextField.text!, bottomText: bottomTextField.text!, originalImage: imagePickerView.image!, memedImage: memedImage)
        // Add it to the memes array in the Application Delegate
        let object = UIApplication.shared.delegate
        let appDelegate = object as! AppDelegate
        appDelegate.memes.append(meme)
        print(appDelegate.memes.count)
    }

Which is then called by the following IBAction:

然后由以下IBAction调用:

@IBAction func shareMeme(_ sender: Any) {
        let memedImage = generateMemedImage()
        let activityVC = UIActivityViewController(activityItems: [memedImage], applicationActivities: nil)

        activityVC.completionWithItemsHandler = {
            activity, completed, items, error in
            if completed {
                self.save()
                self.dismiss(animated: true, completion: nil)
            }
        }
        present(activityVC, animated: true, completion: nil)
    }

I have tried flipping the Bool states in the follow with no luck. Where am I going wrong? Here is a link to the repo as well.

我试过在后面翻转Bool状态,没有运气。我哪里错了?这里也是回购的链接。

2 个解决方案

#1


1  

You are assuming to interact with the default toolbar nested in the NavigationController (navigationController?.setToolbarHidden), but the fact is that you defined a custom UIToolBar inside your Storyboard/ViewController:

您假设与嵌套在NavigationController(navigationController?.setToolbarHidden)中的默认工具栏进行交互,但事实是您在Storyboard / ViewController中定义了一个自定义UIToolBar:

调用函数时无法隐藏iOS工具栏

So to be able to switch the visibility of your custom UIToolBar you should first, attach the outlet doing so:

因此,为了能够切换自定义UIToolBar的可见性,首先应该连接插座:

@IBOutlet weak var toolbar: UIToolbar!

and then whenever you want to hide it:

然后每当你想隐藏它:

UIView.animate(withDuration: 0.25, animations: { self.toolbar.alpha = 0 })

or show it:

或显示:

UIView.animate(withDuration: 0.25, animations: { self.toolbar.alpha = 1 })

#2


1  

It seems like activityVC.completionWithItemsHandler is called asynchronously from a thread other than the main one. Any UI update should always be called from the main thread and doing otherwise usually results in weird behavior.

似乎activityVC.completionWithItemsHandler是从主要线程以外的线程异步调用的。应始终从主线程调用任何UI更新,否则通常会导致奇怪的行为。

In your case, you can try calling save() from the main thread as that is the method calling generateMemedImage(). It should look something like this:

在您的情况下,您可以尝试从主线程调用save(),因为这是调用generateMemedImage()的方法。它应该看起来像这样:

...

if completed {
  DispatchQueue.main.async {
    self.save()
    self.dismiss(animated: true, completion: nil)
  }
}

...

Note that if saving takes too long, your UI may get clunky as the main thread will be blocked while saving. If that is the case, you may need to try a different logic for saving.

请注意,如果保存时间过长,您的UI可能会变得笨重,因为主要线程在保存时将被阻止。如果是这种情况,您可能需要尝试不同的逻辑进行保存。

One other thing you need to be careful about is how you show/hide the toolbar. You are calling navigationController?.setToolbarHidden() twice in the same method, to show and then hide it. However, what you need to take into account is that UI updates are not executed instantly, but rather they are queued in the main thread to be executed after your method returns. So, leaving them as they are right now will not show/hide the toolbar.

您需要注意的另一件事是如何显示/隐藏工具栏。您在同一方法中两次调用navigationController?.setToolbarHidden(),以显示然后隐藏它。但是,您需要考虑的是UI更新不会立即执行,而是在主线程中排队,以便在方法返回后执行。因此,将它们保留为现在不会显示/隐藏工具栏。

This is not really an answer but these are things you need to take into consideration in your implementation.

这不是一个真正的答案,但这些是您在实施中需要考虑的事项。

#1


1  

You are assuming to interact with the default toolbar nested in the NavigationController (navigationController?.setToolbarHidden), but the fact is that you defined a custom UIToolBar inside your Storyboard/ViewController:

您假设与嵌套在NavigationController(navigationController?.setToolbarHidden)中的默认工具栏进行交互,但事实是您在Storyboard / ViewController中定义了一个自定义UIToolBar:

调用函数时无法隐藏iOS工具栏

So to be able to switch the visibility of your custom UIToolBar you should first, attach the outlet doing so:

因此,为了能够切换自定义UIToolBar的可见性,首先应该连接插座:

@IBOutlet weak var toolbar: UIToolbar!

and then whenever you want to hide it:

然后每当你想隐藏它:

UIView.animate(withDuration: 0.25, animations: { self.toolbar.alpha = 0 })

or show it:

或显示:

UIView.animate(withDuration: 0.25, animations: { self.toolbar.alpha = 1 })

#2


1  

It seems like activityVC.completionWithItemsHandler is called asynchronously from a thread other than the main one. Any UI update should always be called from the main thread and doing otherwise usually results in weird behavior.

似乎activityVC.completionWithItemsHandler是从主要线程以外的线程异步调用的。应始终从主线程调用任何UI更新,否则通常会导致奇怪的行为。

In your case, you can try calling save() from the main thread as that is the method calling generateMemedImage(). It should look something like this:

在您的情况下,您可以尝试从主线程调用save(),因为这是调用generateMemedImage()的方法。它应该看起来像这样:

...

if completed {
  DispatchQueue.main.async {
    self.save()
    self.dismiss(animated: true, completion: nil)
  }
}

...

Note that if saving takes too long, your UI may get clunky as the main thread will be blocked while saving. If that is the case, you may need to try a different logic for saving.

请注意,如果保存时间过长,您的UI可能会变得笨重,因为主要线程在保存时将被阻止。如果是这种情况,您可能需要尝试不同的逻辑进行保存。

One other thing you need to be careful about is how you show/hide the toolbar. You are calling navigationController?.setToolbarHidden() twice in the same method, to show and then hide it. However, what you need to take into account is that UI updates are not executed instantly, but rather they are queued in the main thread to be executed after your method returns. So, leaving them as they are right now will not show/hide the toolbar.

您需要注意的另一件事是如何显示/隐藏工具栏。您在同一方法中两次调用navigationController?.setToolbarHidden(),以显示然后隐藏它。但是,您需要考虑的是UI更新不会立即执行,而是在主线程中排队,以便在方法返回后执行。因此,将它们保留为现在不会显示/隐藏工具栏。

This is not really an answer but these are things you need to take into consideration in your implementation.

这不是一个真正的答案,但这些是您在实施中需要考虑的事项。