最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中UIViewController设置背景色为clear但是却还是显示黑色

iOS crifan 6411浏览 0评论

代码:

   override func viewDidLoad() {

        super.viewDidLoad()

        gLog.verbose("self.view.frame=\(self.view.frame)")

        self.view.frame = CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, SwitchListHeight)

        gLog.verbose("self.view.frame=\(self.view.frame)")

        self.view.backgroundColor = UIColor.clearColor()

//        self.view.backgroundColor = nil

        gLog.verbose("self.view.backgroundColor=\(self.view.backgroundColor)")

        //self.view.backgroundColor=Optional(UIDeviceWhiteColorSpace 0 0)

    }

结果:

2016-05-27 15:56:12.078 [Verbose] [main] [SwitchListTableViewController.swift:53] viewDidLoad() > self.view.frame=(0.0, 0.0, 320.0, 568.0)

2016-05-27 15:56:12.079 [Verbose] [main] [SwitchListTableViewController.swift:55] viewDidLoad() > self.view.frame=(0.0, 0.0, 320.0, 88.0)

2016-05-27 15:56:12.081 [Verbose] [main] [SwitchListTableViewController.swift:60] viewDidLoad() > self.view.backgroundColor=Optional(UIDeviceWhiteColorSpace 0 0)

显示出黑色,而不是透明色:

swift uiviewcontroller background clear but show black

iphone – Display clearColor UIViewController over UIViewController – Stack Overflow

改为:

        let switchCustomerTypeListVC = SwitchListTableViewController(titleList: ["我的客户", "已交车客户"])

        switchCustomerTypeListVC.view.backgroundColor = UIColor.clearColor()

        gLog.verbose("self.modalPresentationStyle=\(self.modalPresentationStyle)")

        self.modalPresentationStyle = .CurrentContext

        gLog.verbose("self.modalPresentationStyle=\(self.modalPresentationStyle)")

        self.presentViewController(switchCustomerTypeListVC, animated: true, completion: nil)

问题依旧。

改为:

        let switchCustomerTypeListVC = SwitchListTableViewController(titleList: ["我的客户", "已交车客户"])

        switchCustomerTypeListVC.view.backgroundColor = UIColor.clearColor()

        gLog.verbose("switchCustomerTypeListVC.modalPresentationStyle=\(switchCustomerTypeListVC.modalPresentationStyle)")

        switchCustomerTypeListVC.modalPresentationStyle = .CurrentContext

        gLog.verbose("switchCustomerTypeListVC.modalPresentationStyle=\(switchCustomerTypeListVC.modalPresentationStyle)")

        self.presentViewController(switchCustomerTypeListVC, animated: true, completion: nil)

有点效果了:

至少是状态栏和导航栏是,clear显示出来的了

但是还是有问题

并且去点击的话,还是可以点击到的。。。

改为:

        let switchCustomerTypeListVC = SwitchListTableViewController(titleList: ["我的客户", "已交车客户"])

        switchCustomerTypeListVC.view.backgroundColor = UIColor.clearColor()

        gLog.verbose("switchCustomerTypeListVC.modalPresentationStyle=\(switchCustomerTypeListVC.modalPresentationStyle)")

        //switchCustomerTypeListVC.modalPresentationStyle = .CurrentContext

        switchCustomerTypeListVC.modalPresentationStyle = .OverCurrentContext

        gLog.verbose("switchCustomerTypeListVC.modalPresentationStyle=\(switchCustomerTypeListVC.modalPresentationStyle)")

        self.presentViewController(switchCustomerTypeListVC, animated: true, completion: nil)

终于可以完整显示透明的了:

ios – Swift UIView background color opacity – Stack Overflow

http://stackoverflow.com/questions/27439220/swift-uiview-background-color-opacity

uiviewcontroller – ModalViewController transparent background iOS 8 – Stack Overflow

ios – Swift Modal View Controller with transparent background – Stack Overflow

objective c – iOS: Modal ViewController with transparent background – Stack Overflow

iphone – Display clear colored ViewController over another ViewController in iOS 7 – Stack Overflow

[总结]

好像主要就是:

将要显示出来的VC的modalPresentationStyle,设置为UIModalPresentationStyle.OverCurrentContext

就可以了。

转载请注明:在路上 » [已解决]swift中UIViewController设置背景色为clear但是却还是显示黑色

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (1)

  1. 解决了我的问题
    ning6年前 (2018-08-29)回复
86 queries in 0.186 seconds, using 22.11MB memory