我知道这个话题很流行,但是我对一门编程语言还不太了解,事实是我仍然不明白我把代码放在哪里了.好吧,我会告诉你整个案子:

enter image description here

我试图让一个模式Swift与普通模式略有不同:点击一个按钮,屏幕上会显示ViewController(遵循模式类型),但背景是透明的.仅显示带有标签的蓝色视图.显示此ViewController时,它的背景是透明的,但一旦完成转换,它将保持黑色背景.已经停用了不透明选项,并测试了一些选项,但没有任何问题.

有人能帮我吗?

视频是在模拟机上对 case (https://www.youtube.com/watch?v=wT8Uwmq9yqY)的测试.

我从swift开始,我对如何在Xcode中编程仍然很迷茫,我阅读了一个问题的答案,其中有以下代码可以解决这个问题:

self.presentingViewController.providesPresentationContextTransitionStyle = YES;
self.presentingViewController.definesPresentationContext = YES;
modal.modalPresentationStyle = UIModalPresentationOverCurrentContext;

我把这个代码放在哪里?

推荐答案

您可以这样做:

In your main view controller:

func showModal() {
    let modalViewController = ModalViewController()
    modalViewController.modalPresentationStyle = .overCurrentContext
    presentViewController(modalViewController, animated: true, completion: nil)
}

In your modal view controller:

class ModalViewController: UIViewController {
    override func viewDidLoad() {
        view.backgroundColor = UIColor.clearColor()
        view.opaque = false
    }
}

If you are working with a storyboard:

只需将Kind设置为Present Modally的故事板分段添加到模式视图控制器,然后在此视图控制器上设置以下值:

  • 背景=清晰的 colored颜色
  • Drawing=取消选中不透明复选框
  • 演示=超过当前上下文

正如Crashalot在他的 comments 中指出的那样:确保segue只在PresentationTransition中使用Default.将Current Context用于Presentation会使模式变为黑色,而不是保持透明.

Ios相关问答推荐

如何保持拖动预览与原始项目完全相同?

我想在expo 中使用useSafeAreaInsets来获取paddingbottom,但我得到了一个错误

SwiftUI Divider过大了其父视图

UITableViewCell编程约束问题

如何在iOS 17中拦截UITextView中的链接点击?UITextItemInteraction已弃用

为什么iOS委派在KMM中不能正常工作?

SwiftUI标题/标题文本背景 colored颜色 屏幕宽度

如何删除点击时按钮的不透明动画?

Swift Combine:prepend() before share() 阻止初始接收器执行

var name: String 时 print(nil) 和 print(name) 的区别? = 无

如何将 -fobjc-arc-exceptions 标志正确添加到 XCode 中?

Xcode 不支持 iOS 15.6

DateFormatter 使用 TimeZone 和 Locale 的特定组合返回 nil

如何使用 Swift 从assets资源 中加载特定图像

在 UITextView 中垂直居中文本

当键盘出现在 iOS 中时,将 UIView 上移

SwiftUI - 列表行中的多个按钮

iOS:设备旋转后如何运行函数(Swift)

如何在 Objective-C (iOS) 中的图像上写文本?

正确使用 Alamofire 的 URLRequestConvertible