【问题标题】:How to present ViewController as popup VC with transparent background?如何将 ViewController 呈现为具有透明背景的弹出式 VC?
【发布时间】:2019-12-08 19:25:52
【问题描述】:

我想将 ViewController 呈现为弹出式 ViewController,它工作得很好,但问题是背景颜色变黑,但我想要一个透明的背景颜色。

let vc = self.storyboard?.instantiateViewController(withIdentifier: "ADLVC") as! ListViewController
//remove black screen in background
vc.modalPresentationStyle = .overCurrentContext
//add clear color background
vc.view.backgroundColor = UIColor.black.withAlphaComponent(0.4)
//present modal
self.navigationController?.pushViewController(vc, animated: false)

使用此代码,我可以呈现一个透明的弹出 ViewCcontroller,但是当单击弹出 ViewController viewWillAppear() 中的关闭按钮时没有调用。

//create view controller
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SVC") as! SViewController

    //remove black screen in background
    vc.modalPresentationStyle = .overCurrentContext
    //add clear color background
    vc.view.backgroundColor = UIColor.black.withAlphaComponent(0.4)

    //present modal
    self.present(vc, animated: false, completion: nil)

【问题讨论】:

    标签: ios swift xcode cocoa-touch uiviewcontroller


    【解决方案1】:

    在vc中,使用[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]设置背景颜色并删除vc.modalPresentationStyle = .overCurrentContext,希望可以!

    【讨论】:

    • 请告诉我这两个[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]UIColor.black.withAlphaComponent(0.4)的区别
    • 同理,J 只是以为你设置了视图的 alpha 属性,哈哈,
    【解决方案2】:

    不可能使 UIView 透明,直到它是其他一些 UIVIew 的子视图。

    要实现您的目标,您可以将该视图控制器添加为演示者的子视图控制器。并将子视图控制器的视图作为子视图添加到父视图控制器的视图中。

    看看: 让 childViewController 成为您要呈现的那个。 然后在演示者(父)视图控制器中执行此操作。

    presenter.addChild(childViewController) childViewController.view.backgroundColor = UIColor.clear presenter.view.addSubView(childViewController.view) childViewController.didMove(toParent: presenter)

    您也可以添加动画。 希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      如果您想将您的 ViewController 显示为弹出窗口。你可以使用 UIWindow + modalPresentationStyle + modalTransitionStyle。像这样:

       let storyboard = UIStoryboard(name: "ADLVC", bundle: nil)
          let listViewController = storyboard.instantiateViewController(withIdentifier: "ListViewController")
          listViewController.modalPresentationStyle = .overFullScreen
          listViewController.modalTransitionStyle = .crossDissolve
          self.window?.rootViewController!.present(metarTAFVC, animated: true, completion: nil)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        • 1970-01-01
        相关资源
        最近更新 更多