【问题标题】:UIModalPresentationFormSheet with transparent background?具有透明背景的 UIModalPresentationFormSheet?
【发布时间】:2012-01-11 23:43:17
【问题描述】:

基本上在这里,我希望模态表单是透明的,这样我就可以看到它后面的 UIView(它是全屏的)。

这可能吗?将UIView 设置为不透明并且backgroundColor[UIColor clearColor] 不起作用。

我注意到,当我从 UIModalPresentationFormSheet 切换到另一种演示风格时,我的视图背景确实是透明的。

我哪里出错了?

【问题讨论】:

    标签: ios cocoa-touch ipad uikit


    【解决方案1】:

    这对我有用:

    [modalView.superview setBackgroundcolor:[UIColor clearColor]];
    

    它仍然会透明地使背景视图变暗,但不会用纯灰色覆盖它。

    ..................................................

    iOS 8 更新

    我之前的解决方案不再适用于 iOS 8。 有很多步骤可以让这样的东西适用于 iOS 8。所以我创建了一个很好的可重用解决方案:ModalController https://github.com/koreyhinton/ModalController.

    它真的很容易使用并且会自动关闭。

    let modal = ModalController()
    modal.content = myCustomView
    presentViewController(modal, animated: true, completion: nil)
    

    如果您对实现细节感兴趣,我就是这样做的:

    让您的模态视图控制器占据整个屏幕,主视图的背景将在清晰和半暗之间切换。然后,您将拥有一个内容视图,它将成为以纯色背景颜色为中心的模式。我还使用了演示样式:.OverCurrentContext,我在 init 方法中指定了它。

    var content = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.clearColor()
        content.frame = CGRect(x:0, y:0, width:300, height:300)
        content.backgroundColor = UIColor.blackColor()
        view.addSubview(content)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.7)
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        view.backgroundColor = UIColor.clearColor()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        content!.center = view.center
    }
    

    【讨论】:

      【解决方案2】:

      据我所知,这已经不可能了。以前是这样,但 Apple 已禁用此功能。

      我所做的只是像平常一样创建我的UIViews,并将它们作为子视图添加到我的主视图中。我必须关闭我的表单,然后进行此演示。

      【讨论】:

      • @bneely 不抱歉,我不确定这是什么时候引入的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-31
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-29
      相关资源
      最近更新 更多